x.id c.id. , x.id_contact c.id. ( )
var data = (from c in db.Contacts
from e in db.Emails.Where(x => x.id_contact == c.id).DefaultIfEmpty()
from p in db.Phones.Where(x => x.id_contact == c.id).DefaultIfEmpty()
from t in db.Tags.Where(x => x.id_contact == c.id).DefaultIfEmpty()
select new
{
id = c.id,
phones = p.number,
emails = e.email1,
tags = t.tag1,
firstname = c.firstname,
lastname = c.lastname,
address = c.address,
city = c.city,
bookmarked = c.bookmarked,
notes = c.notes
}).ToList();
return Json(data, JsonRequestBehavior.AllowGet);
"", " " "" , , 1 . :
var data = (from c in db.Contacts
from e in db.Emails.Where(x => x.id_contact == c.id).DefaultIfEmpty()
from p in db.Phones.Where(x => x.id_contact == c.id).DefaultIfEmpty()
from t in db.Tags.Where(x => x.id_contact == c.id).DefaultIfEmpty()
select new
{
id = c.id,
phone = (p != null ? p.number : null),
email = (e != null ? e.email1 : null),
tag = (t != null ? t.tag1 : null),
firstname = c.firstname,
lastname = c.lastname,
address = c.address,
city = c.city,
bookmarked = c.bookmarked,
notes = c.notes
}).ToList();
var data2 = (from i in data
group i by i.id into grp
select new
{
id = grp.Key,
phones = grp.Where(x => x.phone != null).Select(x => x.phone).ToArray(),
emails = grp.Where(x => x.email != null).Select(x => x.email).ToArray(),
tags = grp.Where(x => x.tag != null).Select(x => x.tag).ToArray(),
firstname = grp.First().firstname,
lastname = grp.First().lastname,
address = grp.First().address,
city = grp.First().city,
bookmarked = grp.First().bookmarked,
notes = grp.First().notes
}).ToList();