简体   繁体   English

linq到对象查询以排除0

[英]linq to object query to exclude 0

I have a Linq query as below. 我有一个Linq查询,如下所示。

var DataSource = from m in product
                 select new { Class = m.Class, Id = (new Make(m.Id)).ProductName};

I instantiate a class called Make and fetch the ProductName based on the Id . 我实例化一个名为Make的类,并根据Id获取ProductName Some of the Id's are 0. Is there a way I can exclude all the Id's that are 0? 有些Id为0。有什么办法可以排除所有为0的Id?

Sure, just do this: 当然,只要这样做:

var DataSource = from m in product
                 where m.Id != 0
                 select new
                 {
                     Class = m.Class,
                     Id = (new Make(m.Id)).ProductName
                 };

您正在寻找where子句:

where m.Id != 0
var DataSource = from m in product
                 where m.Id != 0
                 select new
                 {
                     Class = m.Class,
                     Id = (new Make(m.Id)).ProductName
                 };

Try this: 尝试这个:

var DataSource = from m in product
                               where m.Id != 0
                               select new { Class = m.Class, Id = (new Make(m.Id)).ProductName};

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

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