I am using Entity Framework model data to manipulate data from database (CRUD operations). I want to get all data from tables (not just one).

Here is database model:

Database model

I want to get multiple data from all tables.

Currently I am using query displayed bellow but problem with this query is that I got multiple values from Contact tables and other tables displays only one result. Does someone knows why my query is not working and how to get all multiple data from tables.

Here is Query/Function to get all data from 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
            return Json(data, JsonRequestBehavior.AllowGet);

Related posts

Recent Viewed