LINQ:检查字段 (db) 是否包含 ILIST 中的项目?
我正在尝试创建一个扩展方法,我可以转发状态的 IList 并检查它们是否存在,我认为最好的方法是使用 ILIST - 但也许我错了?这是将多个项目传递给方法(列表)的最佳方法吗?它是一个通用列表,因此没有来自对象的转换等。
基本上我有这个作为签名。
public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
IList<BuildingStatuses> buildingStatus)
{
//PSUEDO CODE
//return from v in qry
// where v.Status is IN MY LIST called buildingStatus
// select v;
}
我使用它来称呼它(下面的示例是在我的 TDD 中),它非常有效,值到达了我上面的方法中。
target.GetBuildings().WithStatus(new List<BuildingFilters.BuildingStatuses>()
{ BuildingFilters.BuildingStatuses.Available,
BuildingFilters.BuildingStatuses.Decommissioned });
所以基本上我有我的列表(IList),它到达带有 2 个值的扩展方法,这很棒,但需要在 LINQ 中说我需要说
return from v in qry
where v.Status is IN MY LIST called buildingStatus
select v;
真的很感谢任何帮助,
关于我的扩展方法,它的工作原理与我所做的类似1 但只传递类型 BuildingStatus 因此只有 1...
I am trying to create an extension method that i can forward a IList of statuses and check weather they exist, I thought the best way to do this was an ILIST - but maybe i wrong? Is this the best way to pass multple items to a method - A List? Its a generic LIST so hence no conversion etc from Object.
Basically I have this as a signature.
public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
IList<BuildingStatuses> buildingStatus)
{
//PSUEDO CODE
//return from v in qry
// where v.Status is IN MY LIST called buildingStatus
// select v;
}
and to call it i use (example below is in my TDD), it works great the values arrive in my method above.
target.GetBuildings().WithStatus(new List<BuildingFilters.BuildingStatuses>()
{ BuildingFilters.BuildingStatuses.Available,
BuildingFilters.BuildingStatuses.Decommissioned });
so basically i have my list (IList) it arrives in the extension method with 2 values which is great but need to say in LINQ i need to say
return from v in qry
where v.Status is IN MY LIST called buildingStatus
select v;
Really appreciate any help,
with regards to my extension method, it works as i have done similar 1 but only passing type BuildingStatus hence only 1...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对你有用吗:
would this work for you:
我认为建议的方法是正确的...您还应该记住
IEnumerable.Where
方法。http://msdn.microsoft.com/en-us/library/bb910023。 ASPX
The suggested approaches I believe are correct... you should also keep in mind the
IEnumerable<T>.Where
method.http://msdn.microsoft.com/en-us/library/bb910023.aspx
您可以使用
IEnumerable.Contains
< /a> 扩展方法并让你的方法更加通用:You can use the
IEnumerable<T>.Contains
extension method and allow your method to be more versatile: