LINQ的地方或过滤器C# [英] LINQ where or filter c#

查看:108
本文介绍了LINQ的地方或过滤器C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务返回的集合。这个集合的性质是的StatusCode,这是一个字符串值,可以从0到5的任何地方(不要问我为什么它被设置为一个字符串...我没有这个模式)。起初,我打算使用LINQ过滤此,才明白,是愚蠢的(为什么返回的记录,100%,当我需要20%)和我我的参数存储过程为我做到这一点。



在任何情况下,我写了一个LINQ查询工作,但出于好奇的缘故,我想有一个更好的方式来做到这一点:

  VAR WO =从W的工作订单
其中w.StatusCode ==0
|| w.StatusCode ==1
|| w.StatusCode ==2
选择瓦;

有什么建议?正确答案去最透彻的答案 - 既包括文档,最佳实践等...


解决方案

  VAR WO =从工作订单q其中int.Parse(w.StatusCode)LT; 3选择瓦; 



是做LINQ查询,至少有一个更清洁的方式。


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 it was set up as a string... I didn't schema this). 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.

In any case, I wrote a LINQ query that worked, but for curiosity sake, I imagine there's a better way to do it:

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 q in workOrders where int.Parse(w.StatusCode) < 3 select w;

Is a cleaner way to do the LINQ query, at least.

这篇关于LINQ的地方或过滤器C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆