Linq的内部变量如何赋值给外部变量?

boy2jake 2015-12-14 08:47:34
var supplierDataList = supplierList.Select(supplier => new {
supplierName = supplier,
supplyData = new {
price =
from quoteDate in quoteDateList
join model in modelList.Where(model => model.MB_Supplier == supplier) //依赖supplier
on quoteDate equals model.MB_QuoteDate
into resultList
from result in resultList.DefaultIfEmpty(new MI_MaterialBase())
select result.aaa
val =
from quoteDate in quoteDateList
join model in modelList.Where(model => model.MB_Supplier == supplier) //依赖supplier
on quoteDate equals model.MB_QuoteDate
into resultList
from result in resultList.DefaultIfEmpty(new MI_MaterialBase())
select result.bbb
}
});

问题:如果按上面这样写,重复代码太多。想把Select每项产生的resultList存到外部变量,实现类似下面的效果,请问该如何实现?

伪代码:
var supplierDataList = supplierList.Select(supplier => new {
supplierName = supplier,
supplyData = new {
price =
from quoteDate in quoteDateList
join model in modelList.Where(model => model.MB_Supplier == supplier) //依赖supplier
on quoteDate equals model.MB_QuoteDate
into resultList
from result in resultList.DefaultIfEmpty(new MI_MaterialBase())
select result.aaa

var tempList = resultList;

val =
from result in tempList.DefaultIfEmpty(new MI_MaterialBase())
select result.bbb
}
});
...全文
2019 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianlang_2008 2018-06-07
  • 打赏
  • 举报
回复
看了下,应该是以supplierList为主写外连查询,代码就没这么麻烦了 var supplierDataList=from p in supplierList join model in modelList on p equals model.MB_Supplier into temp from sup in temp.DefaultIfEmpty() join quoteDate in quoteDateList on quoteDate equals model.MB_QuoteDate select new{ supplierName=p, price=quoteDate.aaa, val=quoteDate.bbb };
qq_15148111 2017-10-14
  • 打赏
  • 举报
回复
使用联和查询都查询出来不举行了(一个查询完事)
  • 打赏
  • 举报
回复
刻意 --> 可以
int[] datas = new int[] { 1, 2, 3, 4, 7, 8, 9 };
var query = from x in datas
            let y = from u in datas
                    where u >= x + x
                    select u
            let cnt = y.Count()
            select new
            {
                lst = y,
                avg = y.Average(),
                sum = y.Sum(),
                length = cnt
            };
let 就相当于 var,用在 linq 之中,而不是之外。 可以在 linq 外定义变量并用在 linq 中使用。但是不要轻易使用超过作用域的变量,应该优先使用 let。
  • 打赏
  • 举报
回复
在 linq 中刻意使用 let 语句。例如
int[] datas = new int[] { 1, 2, 3, 4, 7, 8, 9 };
var query = from x in datas
            let y = from u in datas
                    where u >= x + x
                    select x
            let cnt = y.Count()
            select new
            {
                lst = y,
                avg = y.Average(),
                sum = y.Sum(),
                length = cnt
            };
boy2jake 2015-12-14
  • 打赏
  • 举报
回复
求大神分享思路啊~
boy2jake 2015-12-14
  • 打赏
  • 举报
回复
瞅了这么久,也捣鼓了半天,还是说下我的思路吧。这种方式还是有缺陷,有没有大神给出更加优雅的解决方法?

List<MI_MaterialBase> tempList;
var supplierDataList = supplierList.Select(supplier => new {
    supplierName = supplier,
    supplyData = new {
        price = 
            (from quoteDate in quoteDateList
            join model in modelList.Where(model => model.MB_Supplier == supplier)    //依赖supplier
            on quoteDate equals model.MB_QuoteDate
            into resultList
            from result in resultList.DefaultIfEmpty(new MI_MaterialBase())
            select result).Select((item, i)=>{
                            if (i == 0)     //这样判断有个缺陷,无法知道是否到了最后一项,有没有解决办法?
                            {
                                tempList.Clear();
                            }
                            tempList.Add(item);
                            return t.aaa;
                        }),
            }),

        val = tempList.Select(t => { return t.bbb; })
    }
});
boy2jake 2015-12-14
  • 打赏
  • 举报
回复
引用 3 楼 sp1234 的回复:
刻意 --> 可以
int[] datas = new int[] { 1, 2, 3, 4, 7, 8, 9 };
var query = from x in datas
            let y = from u in datas
                    where u >= x + x
                    select u
            let cnt = y.Count()
            select new
            {
                lst = y,
                avg = y.Average(),
                sum = y.Sum(),
                length = cnt
            };
let 就相当于 var,用在 linq 之中,而不是之外。 可以在 linq 外定义变量并用在 linq 中使用。但是不要轻易使用超过作用域的变量,应该优先使用 let。
let y这样只在var query的作用域才有用,在其它的变量中,例如var value中,想要复用之前的let y,就无力了...

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧