Retrieving Multiple Data from Tables Using the Entity Framework Data Model

I use Entity Framework model data to manage data from a database (CRUD operations). I want to get all the data from tables (not just one).

Here is the database model:

Database model

I want to get some data from all tables.

I am currently using the query below, but the problem with this query is that I got several values ​​from the contact tables, and the other tables show only one result. Does anyone know why my query is not working and how to get all the few data from the tables.

Here is the query / function to get all the data from the database:

ContactsEntities db = new ContactsEntities();
        //get all contacts
        public JsonResult GetAll()
        {
            var data = (from c in db.Contacts
                        from e in db.Emails.Where(x => x.id == c.id).DefaultIfEmpty()
                        from p in db.Phones.Where(x => x.id == c.id).DefaultIfEmpty()
                        from t in db.Tags.Where(x => x.id == 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);
        } 
+4
2

:

var test1 = (from c in db.Contacts
                   join e in db.Emails
                       on c.id equals e.id_contact
                   join t in db.Tags
                   on c.id equals t.id_contact
                   join p in db.Phones on c.id equals p.id_contact
                   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();

, , test1 :

 var result = (from contact in test1
                    group contact by contact.id into grp
                    select new
                    {
                        id = grp.Key,
                        firstname = grp.First().firstname,
                        lastname = grp.First().lastname,
                        address = grp.First().address,
                        city = grp.First().city,
                        bookmarked = grp.First().bookmarked,
                        notes = grp.First().notes,
                        phones = grp.Where(x => x.phones != null).Select(x => x.phones).Distinct().ToArray(),
                        emails = grp.Where(x => x.emails != null).Select(x => x.emails).Distinct().ToArray(),
                        tags = grp.Where(x => x.tags != null).Select(x => x.tags).Distinct().ToArray()
                    }).ToList();

, , , :

1-

2- , "id_contact" ,

3- Sql

4- Visual Studio

Relation

var contacts = (from c in db.Contacts
                       select c).ToList();

, .

+6

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();
+1

Source: https://habr.com/ru/post/1569117/


All Articles