Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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# 连接2个表,并使用实体框架为表1中的每条记录检索表2中的前X条记录_C#_Entity Framework - Fatal编程技术网

C# 连接2个表,并使用实体框架为表1中的每条记录检索表2中的前X条记录

C# 连接2个表,并使用实体框架为表1中的每条记录检索表2中的前X条记录,c#,entity-framework,C#,Entity Framework,我正在尝试使用实体框架完成一项看起来非常简单的任务。我有两个小小的SQL表:Customers(Id,Name)和Orders(Id,CustomerId,DateTime)。我需要从数据库中获取数据,并显示一个表,其中包含所有客户和每个客户的前10个订单(如果客户少于10个,则显示所有订单) 很明显,如何以简单的方式获取所需的数据:在一个查询(var customers=dc.customers.ToList())中获取所有客户,,然后进行foreach循环以检索每个客户的订单: var di

我正在尝试使用实体框架完成一项看起来非常简单的任务。我有两个小小的SQL表:
Customers(Id,Name)
Orders(Id,CustomerId,DateTime)
。我需要从数据库中获取数据,并显示一个表,其中包含所有客户和每个客户的前10个订单(如果客户少于10个,则显示所有订单)

很明显,如何以简单的方式获取所需的数据:在一个查询(
var customers=dc.customers.ToList())中获取所有客户,
,然后进行
foreach
循环以检索每个客户的订单:

var dic = new Dictionary<Customer, List<Order>>();
foreach (var customer in customers)
{
    dic.Add(customer, dc.Orders.Where(o => o.CustomerId == customer.Id).OrderBy(o => o.DateTime).Take(10).ToList();
}
var dic=newdictionary();
foreach(客户中的var客户)
{
Add(customer,dc.Orders.Where(o=>o.CustomerId==customer.Id).OrderBy(o=>o.DateTime).Take(10.ToList();
}
但是这种方法使用了太多的数据库查询。如何仅使用一个数据库查询来获得相同的结果?

您可以使用

Dictionary<Customers, List<Orders>> dic = dc.Customers.Join(dc.Orders.OrderBy(o => o.DateTime).Take(10),
                            c => c.Id,
                            o => o.CustomerId,
                            (c, o) => new { Customers = c, Orders = o }
                            )
                            .GroupBy(x => x.Customers)
                            .ToDictionary(x => x.Key, x => x.Select(y=>y.Orders)

                                                            .ToList());
Dictionary dic=dc.Customers.Join(dc.Orders.OrderBy(o=>o.DateTime).Take(10),
c=>c.Id,
o=>o.CustomerId,
(c,o)=>新{Customers=c,Orders=o}
)
.GroupBy(x=>x.Customers)
.ToDictionary(x=>x.Key,x=>x.Select(y=>y.Orders)
.ToList());
左连接

Dictionary<Customers, List<Orders>> dic = (from c in Customers
                                                      join o in Orders.OrderBy(o => o.DateTime).Take(10) on c.Id equals o.CustomerId into j1
                                                      from j2 in j1.DefaultIfEmpty()
                                                      group j2 by c into grouped
                                                      select new { Customers = grouped.Key, Orders = grouped.ToList() })
                                                     .ToDictionary(x => x.Customers, x => x.Orders);
Dictionary dic=(来自客户中的c)
在Orders.OrderBy(o=>o.DateTime)中加入o。将c.Id上的(10)等于o.CustomerId放入j1
从j1.DefaultIfEmpty()中的j2开始
将j2按c分组为分组
选择新{Customers=grouped.Key,Orders=grouped.ToList()})
.ToDictionary(x=>x.客户,x=>x.订单);

这是左连接吗?我正在查看生成的SQL,它似乎提取了所有连接的记录,而不是前10条记录。有没有办法提取数据库端而不是客户端的前10条记录?我更新了答案。现在,before在数据之后提取前10条记录joining@AndreBorges请检查您的查询从整个数据库中获取前10个订单,然后是joi我需要为每个客户接受10份订单。