Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用实体框架数据模型从表中获取多个数据_C#_Sql_Asp.net_Entity Framework - Fatal编程技术网

C# 使用实体框架数据模型从表中获取多个数据

C# 使用实体框架数据模型从表中获取多个数据,c#,sql,asp.net,entity-framework,C#,Sql,Asp.net,Entity Framework,我使用实体框架模型数据来操作数据库CRUD操作中的数据。我想从表中获取所有数据,而不仅仅是一个 以下是数据库模型: 我想从所有表中获取多个数据 目前我正在使用下面显示的查询,但这个查询的问题是我从Contact表中获得了多个值,而其他表只显示一个结果。有人知道为什么我的查询不起作用,以及如何从表中获取所有多个数据吗 以下是从数据库获取所有数据的查询/函数: ContactsEntities db = new ContactsEntities(); //get all contac

我使用实体框架模型数据来操作数据库CRUD操作中的数据。我想从表中获取所有数据,而不仅仅是一个

以下是数据库模型:

我想从所有表中获取多个数据

目前我正在使用下面显示的查询,但这个查询的问题是我从Contact表中获得了多个值,而其他表只显示一个结果。有人知道为什么我的查询不起作用,以及如何从表中获取所有多个数据吗

以下是从数据库获取所有数据的查询/函数:

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);
        } 

您正在将x.id链接到c.id。我想你需要把x.id\U联系人和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);
从select的“电话”、“电子邮件”和“标签”属性判断,我认为您希望每个联系人有一条记录。您可以通过以下方式通过小组实现这一点:

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

我已经在您的模型上进行了测试,它可以工作:

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”上拖动联系人的id

3-在Sql Server上保存图表

4-在Visual Studio中重新创建模型

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

对于每个联系人,它将仅通过关系获取所有相关的电子邮件、电话和标签。

如果外部键是所有其他表中的联系人,则以下代码将在C-EntityFramework6-MVC5中工作:

var conts= db.Contacts;
db.Entry(conts).Collection(c => c.Emails).Load();
// If you retrieve an email, use "Reference" instead of "Collection"
实体框架文件:

using (var context = new BloggingContext())
{
   var post = context.Posts.Find(2);

   // Load the blog related to a given post
   context.Entry(post).Reference(p => p.Blog).Load();

   // Load the blog related to a given post using a string
   context.Entry(post).Reference("Blog").Load();

   var blog = context.Blogs.Find(1);

   // Load the posts related to a given blog
   context.Entry(blog).Collection(p => p.Posts).Load();

   // Load the posts related to a given blog
   // using a string to specify the relationship
   context.Entry(blog).Collection("Posts").Load();
}

为什么你们不在你们的实体之间建立关系?我将在后面添加它们。我刚开始使用ASP.NET和MS SQL server,所以我目前不知道如何做到这一点。此外,我还需要完成其他工作,目前@ariaUse join的人际关系并不是那么重要,因为id\u联系人是相关的。我在联系人表中没有电子邮件id、电话id和标签id。你说得对。反过来说。编辑回复。谢谢您的帮助。我使用更新的代码,但我得到相同的行4行相同的数据。4行,但与不同的联系信息可能。。。我更新了我的回复,对每个联系人的结果进行分组。我有4行,其中包含来自联系人的相同数据和每个表中的不同日期数据,这些数据位于表中,如电子邮件、电话或标签。是否有方法将来自电子邮件/电话/标签的结果分组到数组中,并将该数组放入联系人列表中?谢谢您的回答。您的代码正在运行,但我从Contacts获得了4个结果,其中包含相同的数据,除了Contacts之外,每个表的数据都不同。有没有办法将电子邮件/标签/电话数据分组到数组并发送到前端?我尝试了您的代码,并向表中添加了外键,但我没有从电话/标签或电子邮件表中获取所有数据…谢谢@aria。我添加了你的代码,它的工作很好,但有一个问题。它只显示一个结果。我需要联系人表中的所有数据。是的,到第二次查询时,他们在子表中没有详细信息的联系人将不会显示。让我们。