Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. namespace ConsoleLinq
  2. {
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Collection<ElementBase> ELementList = new Collection<ElementBase>();
  10. ElementBase elementObjekt = new ElementBase { Name = "Node1" };
  11. elementObjekt.DiagramListe.Add(new Diagram { Name = "Diagram1", LayerName = "Layer2" });
  12. elementObjekt.DiagramListe.Add(new Diagram { Name = "Diagram2", LayerName = "Layer1" });
  13. ELementList.Add(elementObjekt);
  14. elementObjekt = new ElementBase { Name = "Node2" };
  15. elementObjekt.DiagramListe.Add(new Diagram { Name = "Diagram1", LayerName = "Layer1" });
  16. ELementList.Add(elementObjekt);
  17. elementObjekt = new ElementBase { Name = "Node3" };
  18. elementObjekt.DiagramListe.Add(new Diagram { Name = "Diagram2", LayerName = "Layer1" });
  19. ELementList.Add(elementObjekt);
  20.  
  21. // is there a way to select any item in ELementList wich are "Diagram1" and sort the items by LayerName?
  22. var searchList = (from item in ELementList
  23. where item.DiagramListe.GetDiagram("Diagram1") != null
  24. select item).ToList();
  25. // output should
  26. // searchList[0].Name = "Node2";
  27. // searchList[1].Name = "Node1";
  28.  
  29. foreach (var item in searchList) item.DoSomething();
  30. }
  31. }
  32.  
  33. public class ElementBase
  34. {
  35. public string Name { get; set; }
  36.  
  37. public DiagramListe DiagramListe = new DiagramListe();
  38.  
  39. public void DoSomething() { }
  40. }
  41.  
  42. public class DiagramListe : Collection<Diagram>
  43. {
  44. public Diagram GetDiagram(string diagramName)
  45. => (from obj in this
  46. where obj.Name == diagramName
  47. select obj).FirstOrDefault();
  48. }
  49.  
  50. public class Diagram
  51. {
  52. public string Name { get; set; }
  53. public string LayerName { get; set; }
  54. }
  55.  
  56. }
Add Comment
Please, Sign In to add comment