Answers:
我不确定您在寻找什么,但是这个程序:
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);
}
产生预期的输出:
one: open
two: closed
four: open
five: closed
该程序比较枚举的字符串表示形式,并产生相同的输出:
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 ();
}
我创建了此扩展方法,以将一个IEnumerable转换为另一个IEnumerable,但是我不确定它的效率如何。它可能只是在后台创建一个列表。
public static IEnumerable <TResult> ConvertEach (IEnumerable <TSource> sources, Func <TSource,TResult> convert)
{
foreach ( TSource source in sources )
yield return convert (source);
}
然后,您可以将where子句更改为:
where statusList.ConvertEach <string> (status => status.GetCharValue()).
Contains (v.Status)
并跳过创建List<string>
与ConvertAll ()
开头。
如果我理解正确,则需要将“建筑物”列表中存储的类型(字符值)转换为“建筑物状态”列表中存储的类型(枚举)。
(对于建筑物列表//字符值//中的每个状态,该状态是否存在于建筑物状态列表//枚举值//中)
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;
}
Building
,但这已经被破坏了。foreach(Enum value in Enum.GetValues(typeof(buildingStatus)))
废话
Contains()
方法,然后我意识到应该采用这种方法Any()
。+1