简体   繁体   English

LINQ where或过滤c#

[英]LINQ where or filter c#

I have a collection being returned by a web service. 我有一个Web服务返回的集合。 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 it was set up as a string... I didn't schema this). 此集合的一个属性是“ StatusCode”,它是一个字符串值,范围可以是0到5(不要问我为什么将它设置为字符串...我没有对此进行模式化)。 Initially I was going to use LINQ to filter this, then realized that was stupid (why return 100% of the records when I need 20%) and I parameterized my Stored Proc to do this for me. 最初,我打算使用LINQ对此进行过滤,然后意识到这很愚蠢(为什么当我需要20%的记录时才返回100%的记录),所以我对我的Stored Proc进行了参数设置。

In any case, I wrote a LINQ query that worked, but for curiosity sake, I imagine there's a better way to do it: 无论如何,我编写了一个有效的LINQ查询,但出于好奇的缘故,我想有一种更好的方法:

var wo = from w in workOrders
    where w.StatusCode == "0"
       || w.StatusCode == "1"
       || w.StatusCode == "2"
    select w;

Any suggestions? 有什么建议么? Correct answer goes to most thorough answer - including either documentation, best practice, etc... 正确答案是最彻底的答案-包括文档,最佳做法等。

var wo = from w in workOrders 
         where new[]{"0", "1", "2"}.Contains(w.StatusCode)
         select w;

BTW, if you were using an ORM, you could do it in LINQ (like the above) and only pull the 20% from the database ;) 顺便说一句,如果您使用的是ORM,则可以在LINQ中进行操作(如上述操作),并且只从数据库中提取20%的;)

var wo = from q in workOrders where int.Parse(w.StatusCode) < 3 select w;

至少是一种更清洁的方式执行LINQ查询。

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<T>(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). 它适用于任何类型(只要您创建良好的equals方法)。 Any value type for sure. 当然,任何值类型。

You could define an IsIn() generic Extension Method: 您可以定义一个IsIn()通用扩展方法:

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

Then you could write your query like this: 然后,您可以这样编写查询:

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

Your method is ok. 您的方法还可以。 I don't believe parsing or lookup collection is neccessary for 3 string values (however it certainly isn't wrong). 我不认为解析或查找集合对于3个字符串值都是必需的(但是肯定没有错)。 With parsing I'd be afraid of empty strings or nulls (even tho we know that there should only be strings 0-5). 使用解析时,我会担心空字符串或空值(甚至我们都知道应该只有字符串0-5)。

If you are anticipating filtering like this on more places you should probably create method (or extension) on WorkOrder that would determine current state more expressively like 如果您打算在更多地方进行这样的过滤,则可能应该在WorkOrder上创建方法(或扩展名),以便更明确地确定当前状态,例如

public static bool IsNotCompleted(this WorkOrder workOrder) 
{
    return workOrder.Status == "0" || workOrder.Status == "1" || workOrder.Status == "2";
}

and then 接着

var wo = from o in workOrders where o.IsNotCompleted() select o;

or (I personally prefer this sytaxe) 或(我个人更喜欢此sytaxe)

var wo = workOrders.Where(o => o.IsNotCompleted());

Using extension to describe state like this will increase readability and it will be much easier to add/remove status codes in future. 使用扩展名来描述这样的状态将提高可读性,并且将来添加/删除状态代码会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM