LINQ:检查字段 (db) 是否包含 ILIST 中的项目?

发布于 2024-08-07 05:27:13 字数 1059 浏览 3 评论 0原文

我正在尝试创建一个扩展方法,我可以转发状态的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零崎曲识 2024-08-14 05:27:13

这对你有用吗:

return from v in qry
       where buildingStatus.Contains(v.Status)
       select v;

would this work for you:

return from v in qry
       where buildingStatus.Contains(v.Status)
       select v;
东风软 2024-08-14 05:27:13

我认为建议的方法是正确的...您还应该记住 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

放赐 2024-08-14 05:27:13

您可以使用 IEnumerable.Contains< /a> 扩展方法并让你的方法更加通用:

public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
          IEnumerable<BuildingStatuses> buildingStatus)
{

    return from v in qry
           where buildingStatus.Contains(v.Status)
           select v;
}

You can use the IEnumerable<T>.Contains extension method and allow your method to be more versatile:

public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
          IEnumerable<BuildingStatuses> buildingStatus)
{

    return from v in qry
           where buildingStatus.Contains(v.Status)
           select v;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文