Guest User

Untitled

a guest
Dec 15th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. ContactsEntities db = new ContactsEntities();
  2. //get all contacts
  3. public JsonResult GetAll()
  4. {
  5. var data = (from c in db.Contacts
  6. from e in db.Emails.Where(x => x.id == c.id).DefaultIfEmpty()
  7. from p in db.Phones.Where(x => x.id == c.id).DefaultIfEmpty()
  8. from t in db.Tags.Where(x => x.id == c.id).DefaultIfEmpty()
  9. select new
  10. {
  11. id = c.id,
  12. phones = p.number,
  13. emails = e.email1,
  14. tags = t.tag1,
  15. firstname = c.firstname,
  16. lastname = c.lastname,
  17. address = c.address,
  18. city = c.city,
  19. bookmarked = c.bookmarked,
  20. notes = c.notes
  21. }).ToList();
  22. return Json(data, JsonRequestBehavior.AllowGet);
  23. }
  24.  
  25. var test1 = (from c in db.Contacts
  26. join e in db.Emails
  27. on c.id equals e.id_contact
  28. join t in db.Tags
  29. on c.id equals t.id_contact
  30. join p in db.Phones on c.id equals p.id_contact
  31. select new
  32. {
  33. id = c.id,
  34. phones = p.number,
  35. emails = e.email1,
  36. tags = t.tag1,
  37. firstname = c.firstname,
  38. lastname = c.lastname,
  39. address = c.address,
  40. city = c.city,
  41. bookmarked = c.bookmarked,
  42. notes = c.notes
  43. }).ToList();
  44.  
  45. var result = (from contact in test1
  46. group contact by contact.id into grp
  47. select new
  48. {
  49. id = grp.Key,
  50. firstname = grp.First().firstname,
  51. lastname = grp.First().lastname,
  52. address = grp.First().address,
  53. city = grp.First().city,
  54. bookmarked = grp.First().bookmarked,
  55. notes = grp.First().notes,
  56. phones = grp.Where(x => x.phones != null).Select(x => x.phones).Distinct().ToArray(),
  57. emails = grp.Where(x => x.emails != null).Select(x => x.emails).Distinct().ToArray(),
  58. tags = grp.Where(x => x.tags != null).Select(x => x.tags).Distinct().ToArray()
  59. }).ToList();
  60.  
  61. var contacts = (from c in db.Contacts
  62. select c).ToList();
  63.  
  64. var data = (from c in db.Contacts
  65. from e in db.Emails.Where(x => x.id_contact == c.id).DefaultIfEmpty()
  66. from p in db.Phones.Where(x => x.id_contact == c.id).DefaultIfEmpty()
  67. from t in db.Tags.Where(x => x.id_contact == c.id).DefaultIfEmpty()
  68. select new
  69. {
  70. id = c.id,
  71. phones = p.number,
  72. emails = e.email1,
  73. tags = t.tag1,
  74. firstname = c.firstname,
  75. lastname = c.lastname,
  76. address = c.address,
  77. city = c.city,
  78. bookmarked = c.bookmarked,
  79. notes = c.notes
  80. }).ToList();
  81. return Json(data, JsonRequestBehavior.AllowGet);
  82.  
  83. var data = (from c in db.Contacts
  84. from e in db.Emails.Where(x => x.id_contact == c.id).DefaultIfEmpty()
  85. from p in db.Phones.Where(x => x.id_contact == c.id).DefaultIfEmpty()
  86. from t in db.Tags.Where(x => x.id_contact == c.id).DefaultIfEmpty()
  87. select new
  88. {
  89. id = c.id,
  90. phone = (p != null ? p.number : null),
  91. email = (e != null ? e.email1 : null),
  92. tag = (t != null ? t.tag1 : null),
  93. firstname = c.firstname,
  94. lastname = c.lastname,
  95. address = c.address,
  96. city = c.city,
  97. bookmarked = c.bookmarked,
  98. notes = c.notes
  99. }).ToList();
  100.  
  101. var data2 = (from i in data
  102. group i by i.id into grp
  103. select new
  104. {
  105. id = grp.Key,
  106. phones = grp.Where(x => x.phone != null).Select(x => x.phone).ToArray(),
  107. emails = grp.Where(x => x.email != null).Select(x => x.email).ToArray(),
  108. tags = grp.Where(x => x.tag != null).Select(x => x.tag).ToArray(),
  109. firstname = grp.First().firstname,
  110. lastname = grp.First().lastname,
  111. address = grp.First().address,
  112. city = grp.First().city,
  113. bookmarked = grp.First().bookmarked,
  114. notes = grp.First().notes
  115. }).ToList();
  116.  
  117. var conts= db.Contacts;
  118. db.Entry(conts).Collection(c => c.Emails).Load();
  119. // If you retrieve an email, use "Reference" instead of "Collection"
  120.  
  121. using (var context = new BloggingContext())
  122. {
  123. var post = context.Posts.Find(2);
  124.  
  125. // Load the blog related to a given post
  126. context.Entry(post).Reference(p => p.Blog).Load();
  127.  
  128. // Load the blog related to a given post using a string
  129. context.Entry(post).Reference("Blog").Load();
  130.  
  131. var blog = context.Blogs.Find(1);
  132.  
  133. // Load the posts related to a given blog
  134. context.Entry(blog).Collection(p => p.Posts).Load();
  135.  
  136. // Load the posts related to a given blog
  137. // using a string to specify the relationship
  138. context.Entry(blog).Collection("Posts").Load();
  139. }
Add Comment
Please, Sign In to add comment