LINQ: “包含”和一个 Lambda 查询

我有一个叫做 buildingStatusList<BuildingStatus>。我想检查它是否包含一个其字符代码(由 GetCharCode()返回)等于某个变量 v.Status的状态。

是否有某种方法可以按照下面的(非编译)代码行来做到这一点?

buildingStatus.Contains(item => item.GetCharValue() == v.Status)
272808 次浏览

使用 强 > Any()代替 Contains():

buildingStatus.Any(item => item.GetCharValue() == v.Status)

Linq 扩展方法 任何可以为您工作..。

buildingStatus.Any(item => item.GetCharValue() == v.Status)

我不确定你到底在找什么,但这个程序:

    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 ();
}

我创建这个扩展方法是为了将一个 IEnumable 转换为另一个,但我不确定它的效率如何; 它可能只是在幕后创建一个列表。

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)

并跳过在开始时用 ConvertAll ()创建 List<string>

如果我理解正确,您需要转换存储的类型(char 值) 存储在 buildingStatus 列表中的类型(枚举)。

(对于 Building 列表中的每个状态//字符值//, 状态是否存在于 buildingStatus 列表//enum 值//)中

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;
}

下面是如何使用 Contains来实现你想要的:

这将返回一个布尔值。

var depthead = (from s in db.M_Users
join m in db.M_User_Types on s.F_User_Type equals m.UserType_Id
where m.UserType_Name.ToUpper().Trim().Contains("DEPARTMENT HEAD")
select new {s.FullName,s.F_User_Type,s.userId,s.UserCode }
).OrderBy(d => d.userId).ToList();


Model.AvailableDeptHead.Add(new SelectListItem { Text = "Select", Value = "0" });
for (int i = 0; i < depthead.Count; i++)
Model.AvailableDeptHead.Add(new SelectListItem { Text = depthead[i].UserCode + " - " + depthead[i].FullName, Value = Convert.ToString(depthead[i].userId) });