LINQ where or filter c#

后端 未结 5 1339
故里飘歌
故里飘歌 2021-01-24 04:29

I have a collection being returned by a web service. A property of this collection is \"StatusCode\" which is a string value that can be anywhere from 0 to 5 (don\'t ask me why

5条回答
  •  长情又很酷
    2021-01-24 04:52

    I recently wrote a blog post about a method using extension methods and params.

    By adding this extension method to your code:

    public static bool IsIn(this T source, params T[] values)
    {
        return values.Contains(source);
    }
    

    you can perform your search like this:

    var wo = from w in workOrders
        where w.StatusCode.IsIn("0", "1", "2")
        select w;
    

    It works on any type (as long as you create a good equals method). Any value type for sure.

提交回复
热议问题