8,497
社区成员
发帖
与我相关
我的任务
分享
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。
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
};
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; })
}
});