C# where子句中的for循环
我试图用两个条件通过LINQ计算值,其中一个条件恰好包含foreach循环 我有一个整数列表statusList(存储StatCatId) 根据这个列表中的值,我想在LINQ语句中添加一个where条件C# where子句中的for循环,c#,linq,C#,Linq,我试图用两个条件通过LINQ计算值,其中一个条件恰好包含foreach循环 我有一个整数列表statusList(存储StatCatId) 根据这个列表中的值,我想在LINQ语句中添加一个where条件 var sListings = (from l in _colMgr.Listings.Values.AsEnumerable() where (l.SystemPrice ?? 0) > 0 && //
var sListings = (from l in _colMgr.Listings.Values.AsEnumerable()
where (l.SystemPrice ?? 0) > 0 &&
// Need to add condition like foreach(s in statusList) if l.StatCatId == s
select l).ToList();
您可能正在查找
Contains
方法:
var sListings =
(
from l in _colMgr.Listings.Values.AsEnumerable()
where (l.SystemPrice ?? 0) > 0
&& statusList.Contains(l.StatCatId)
select l
).ToList();
您可能正在查找
Contains
方法:
var sListings =
(
from l in _colMgr.Listings.Values.AsEnumerable()
where (l.SystemPrice ?? 0) > 0
&& statusList.Contains(l.StatCatId)
select l
).ToList();
根据计算的复杂性,可以使用Contains、SelectMany、Sum等,也可以在LINQ调用中插入自己的Func
class Program
{
static void Main(string[] args)
{
var data = new List<DataPoint>
{
new DataPoint(){Condition = true, Value = 0, Values = new List<int>{1,2,3}},
new DataPoint(){Condition = false, Value = 10, Values = new List<int>{}},
new DataPoint(){Condition = true, Value = 0, Values = new List<int>{4,5}},
new DataPoint(){Condition = false, Value = 20, Values = new List<int>{}}
};
var sum = data.Sum(s=>s.Condition?s.Values.Sum():s.Value);
}
}
internal class DataPoint
{
public bool Condition { get; set; }
public int Value { get; set; }
public List<int> Values { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
var数据=新列表
{
new DataPoint(){Condition=true,Value=0,Values=new List{1,2,3}},
new DataPoint(){Condition=false,Value=10,Values=new List{},
new DataPoint(){Condition=true,Value=0,Values=new List{4,5}},
new DataPoint(){Condition=false,Value=20,Values=new List{}
};
var sum=data.sum(s=>s.Condition?s.Values.sum():s.Value);
}
}
内部类数据点
{
公共布尔条件{get;set;}
公共int值{get;set;}
公共列表值{get;set;}
}
或者用Func
Func=point=>point.Condition?point.Values.Sum():point.Value;
var sum=data.sum(s=>func(s));
或者,如果Func需要多个步骤:
Func<DataPoint, int> func = point =>
{
var retValue = point.Condition ? point.Values.Sum() : point.Value;
return retValue;
};
var sum = data.Sum(s => func(s));
Func=point=>
{
var retValue=point.Condition?point.Values.Sum():point.Value;
返回值;
};
var sum=data.sum(s=>func(s));
根据计算的复杂性,您可以使用Contains、SelectMany、Sum等,也可以在LINQ调用中插入自己的Func
class Program
{
static void Main(string[] args)
{
var data = new List<DataPoint>
{
new DataPoint(){Condition = true, Value = 0, Values = new List<int>{1,2,3}},
new DataPoint(){Condition = false, Value = 10, Values = new List<int>{}},
new DataPoint(){Condition = true, Value = 0, Values = new List<int>{4,5}},
new DataPoint(){Condition = false, Value = 20, Values = new List<int>{}}
};
var sum = data.Sum(s=>s.Condition?s.Values.Sum():s.Value);
}
}
internal class DataPoint
{
public bool Condition { get; set; }
public int Value { get; set; }
public List<int> Values { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
var数据=新列表
{
new DataPoint(){Condition=true,Value=0,Values=new List{1,2,3}},
new DataPoint(){Condition=false,Value=10,Values=new List{},
new DataPoint(){Condition=true,Value=0,Values=new List{4,5}},
new DataPoint(){Condition=false,Value=20,Values=new List{}
};
var sum=data.sum(s=>s.Condition?s.Values.sum():s.Value);
}
}
内部类数据点
{
公共布尔条件{get;set;}
公共int值{get;set;}
公共列表值{get;set;}
}
或者用Func
Func=point=>point.Condition?point.Values.Sum():point.Value;
var sum=data.sum(s=>func(s));
或者,如果Func需要多个步骤:
Func<DataPoint, int> func = point =>
{
var retValue = point.Condition ? point.Values.Sum() : point.Value;
return retValue;
};
var sum = data.Sum(s => func(s));
Func=point=>
{
var retValue=point.Condition?point.Values.Sum():point.Value;
返回值;
};
var sum=data.sum(s=>func(s));
连接子句的另一种方式
var sListings =
(
from l in _colMgr.Listings.Values.AsEnumerable()
join s in statusList on l.StatCatId equals s
where (l.SystemPrice ?? 0) > 0
select l
).ToList();
另一种方法是使用
join子句
var sListings =
(
from l in _colMgr.Listings.Values.AsEnumerable()
join s in statusList on l.StatCatId equals s
where (l.SystemPrice ?? 0) > 0
select l
).ToList();