Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. Consider following class which stores the information about external urls.
  2.  
  3. 1: class LinkInfo
  4. 2: {
  5. 3:     public int    Ord { get; set; }
  6. 4:     public string Url { get; set; }
  7. 5: }
  8.  
  9. In your application you somehow retrieve the list of such items:
  10.  
  11. 1: List<LinkInfo> l = new List<LinkInfo>()
  12. 2: {
  13. 3:     new LinkInfo() { Ord = 1, Url = "" },
  14. 4:     new LinkInfo() { Ord = 2, Url = "test1" },
  15. 5:     new LinkInfo() { Ord = 3, Url = "test1" },
  16. 6:     new LinkInfo() { Ord = 4, Url = "test2" },
  17. 7:     new LinkInfo() { Ord = 5, Url = "" }
  18. 8: };
  19.  
  20. Your goal is to use Linq to filter this list in a special way:
  21.     if the Url is empty - the item is always returned,
  22.     if the Url is nonempty - only one item with such Url is returned.
  23.  
  24. In the above case the Linq expression should return:
  25. 1: {
  26. 2:     new LinkInfo() { Ord = 1, Url = "" },
  27. 3:     new LinkInfo() { Ord = 2, Url = "test1" },
  28. 4:     new LinkInfo() { Ord = 4, Url = "test2" },
  29. 5:     new LinkInfo() { Ord = 5, Url = "" }
  30. 6: };
  31.  
  32. or
  33.  
  34. 1: {
  35. 2:     new LinkInfo() { Ord = 1, Url = "" },
  36. 3:     new LinkInfo() { Ord = 3, Url = "test1" },
  37. 4:     new LinkInfo() { Ord = 4, Url = "test2" },
  38. 5:     new LinkInfo() { Ord = 5, Url = "" }
  39. 6: }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement