LINQ踩坑记录

 

1、join into 解析为groupjoin(组连接),但是直接对iqueryable做组连接报错,比对了一下之前一个项目中似乎是可以的,是不是环境不一样的原因呢,有待考证,当前的解决办法如下

//只能通过异步的方式取LIST再groupjoin不然不成功,

//控制器在调用这个异步方法时也要返回 Task或者await
var inorders = await context.t_inorders.AsNoTracking().ToListAsync();
var inorderdetails = await context.t_inorderdetais.AsNoTracking().ToListAsync();
var products = await context.t_products.AsNoTracking().ToListAsync();


var queryGP = from inorder in inorders
where inorder.state == 1 && inorder.orderstate < 4 && inorder.orderstate > 1
join detail_n in
(from detail in inorderdetails
join product in products on detail.productid equals product.id
select new
{
detail.productid,
detail.orderid,
detail.alreadybindcount,
detail.alreadycount,
detail.productnum,
product.productname

}) on inorder.id equals detail_n.orderid into details
select new
{
inorder.ordercode,
inorder.tripcode,
inorder.suppliercode,
inorder.suppliername,
details
};
var obj = queryGP.ToList();

 

2、如果是要借助join into实现左联接的功能却不受影响,是因为没有被解析为groupjoin的原因吗?


var A = context.t_inorders.AsNoTracking<t_inorder>();
var B = context.t_inorderdetais.AsNoTracking<t_inorderdetai>();
var C = context.t_products.AsNoTracking<t_product>();

var queryGP1 = from inorder in A
where inorder.state == 1 && inorder.orderstate < 4 && inorder.orderstate > 1
join detail_n in
(from detail in B
join product in C on detail.productid equals product.id
select new
{
detail.productid,
detail.orderid,
detail.alreadybindcount,
detail.alreadycount,
detail.productnum,
product.productname

}) on inorder.id equals detail_n.orderid into details
from d1 in details.DefaultIfEmpty()
select new
{
inorder.ordercode,
inorder.tripcode,
inorder.suppliercode,
inorder.suppliername
};
var obj1 = queryGP1.ToList();

posted on 2020-12-14 12:17  yzmn  阅读(116)  评论(0编辑  收藏  举报

导航