C# LINQ:“包含”和 Lambda 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1566439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ: "contains" and a Lambda query
提问by mark smith
I have a List<BuildingStatus>
called buildingStatus
. I'd like to check whether it contains a status whose char code (returned by GetCharCode()
) equals some variable, v.Status
.
我有一个List<BuildingStatus>
叫buildingStatus
. 我想检查它是否包含字符代码(由 返回GetCharCode()
)等于某个变量的状态,v.Status
.
Is there some way of doing this, along the lines of the (non-compiling) code below?
有没有办法做到这一点,沿着下面的(非编译)代码行?
buildingStatus.Contains(item => item.GetCharValue() == v.Status)
采纳答案by Rex M
回答by flq
The Linq extension method Anycould work for you...
Linq 扩展方法Any可以为您工作...
buildingStatus.Any(item => item.GetCharValue() == v.Status)
回答by XXXXX
I'm not sure precisely what you're looking for, but this program:
我不确定你在找什么,但这个程序:
public class Building
{
public enum StatusType
{
open,
closed,
weird,
};
public string Name { get; set; }
public StatusType Status { get; set; }
}
public static List <Building> buildingList = new List<Building> ()
{
new Building () { Name = "one", Status = Building.StatusType.open },
new Building () { Name = "two", Status = Building.StatusType.closed },
new Building () { Name = "three", Status = Building.StatusType.weird },
new Building () { Name = "four", Status = Building.StatusType.open },
new Building () { Name = "five", Status = Building.StatusType.closed },
new Building () { Name = "six", Status = Building.StatusType.weird },
};
static void Main (string [] args)
{
var statusList = new List<Building.StatusType> () { Building.StatusType.open, Building.StatusType.closed };
var q = from building in buildingList
where statusList.Contains (building.Status)
select building;
foreach ( var b in q )
Console.WriteLine ("{0}: {1}", b.Name, b.Status);
}
produces the expected output:
产生预期的输出:
one: open
two: closed
four: open
five: closed
This program compares a string representation of the enum and produces the same output:
该程序比较枚举的字符串表示并产生相同的输出:
public class Building
{
public enum StatusType
{
open,
closed,
weird,
};
public string Name { get; set; }
public string Status { get; set; }
}
public static List <Building> buildingList = new List<Building> ()
{
new Building () { Name = "one", Status = "open" },
new Building () { Name = "two", Status = "closed" },
new Building () { Name = "three", Status = "weird" },
new Building () { Name = "four", Status = "open" },
new Building () { Name = "five", Status = "closed" },
new Building () { Name = "six", Status = "weird" },
};
static void Main (string [] args)
{
var statusList = new List<Building.StatusType> () { Building.StatusType.open, Building.StatusType.closed };
var statusStringList = statusList.ConvertAll <string> (st => st.ToString ());
var q = from building in buildingList
where statusStringList.Contains (building.Status)
select building;
foreach ( var b in q )
Console.WriteLine ("{0}: {1}", b.Name, b.Status);
Console.ReadKey ();
}
I created this extension method to convert one IEnumerable to another, but I'm not sure how efficient it is; it may just create a list behind the scenes.
我创建了这个扩展方法来将一个 IEnumerable 转换为另一个,但我不确定它的效率如何;它可能只是在幕后创建一个列表。
public static IEnumerable <TResult> ConvertEach (IEnumerable <TSource> sources, Func <TSource,TResult> convert)
{
foreach ( TSource source in sources )
yield return convert (source);
}
Then you can change the where clause to:
然后您可以将 where 子句更改为:
where statusList.ConvertEach <string> (status => status.GetCharValue()).
Contains (v.Status)
and skip creating the List<string>
with ConvertAll ()
at the beginning.
并跳过在开头创建List<string>
with ConvertAll ()
。
回答by psabela
If I understand correctly, you need to convert the type (char value) that you store in Building list to the type (enum) that you store in buildingStatus list.
如果我理解正确,您需要将您存储在 Building 列表中的类型(char 值)转换为您存储在 buildingStatus 列表中的类型(枚举)。
(For each status in the Building list//character value//, does the status exists in the buildingStatus list//enum value//)
(对于Building列表中的每个状态//字符值//,是否在buildingStatus列表中存在状态//枚举值//)
public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
IList<BuildingStatuses> buildingStatus)
{
return from v in qry
where ContainsStatus(v.Status)
select v;
}
private bool ContainsStatus(v.Status)
{
foreach(Enum value in Enum.GetValues(typeof(buildingStatus)))
{
If v.Status == value.GetCharValue();
return true;
}
return false;
}
回答by Soft-Brain
Here is how you can use Contains
to achieve what you want:
以下是您可以如何使用Contains
来实现您想要的:
buildingStatus.Select(item => item.GetCharValue()).Contains(v.Status)
this will return a Boolean value.
buildingStatus.Select(item => item.GetCharValue()).Contains(v.Status)
这将返回一个布尔值。