仅当变量具有值时,才在Linq中使用“where”语句

时间:2012-08-24 18:29:23

标签: c# asp.net linq

我有一个创建列表的linq查询。我只想在where使用第二个affiliationID != 0子句。有什么提示吗?

var associations = (from a in ftjent.Associations
                    join ap in ftjent.AssociationProducts on a.AssociationID equals ap.AssociationID
                    where ap.Product.Name == productName
                    where a.AffiliationID == affiliationID
                    select new
                        {
                            a.Acronym,
                            a.AssociationID,
                            a.Name
                         }
                     ).Distinct().OrderBy(assoc => assoc.Acronym);

4 个答案:

答案 0 :(得分:3)

affiliationId与0进行比较,如果它没有值,则where将始终评估为true,并且不会进行第二次比较:

var associations = (from a in ftjent.Associations
                join ap in ftjent.AssociationProducts on a.AssociationID equals ap.AssociationID
                where ap.Product.Name == productName
                where (affiliationId == 0) ||  a.AffiliationID == affiliationID
                select new
                    {
                        a.Acronym,
                        a.AssociationID,
                        a.Name
                     }
                 ).Distinct().OrderBy(assoc => assoc.Acronym);

答案 1 :(得分:0)

var associations = (from a in ftjent.Associations
                join ap in ftjent.AssociationProducts on a.AssociationID equals ap.AssociationID
                where ap.Product.Name == productName
                && (a.AffiliationID == affiliationID
                   || affiliationID == null || affiliationID == 0)
                select new
                    {
                        a.Acronym,
                        a.AssociationID,
                        a.Name
                     }
                 ).Distinct().OrderBy(assoc => assoc.Acronym);

基本上,不是试图只在一个条件下附加条件,这些类型问题的常见解决方案(这是常见的,相信我)是将条件否定为“或”,给出查询替代;这里,记录的affiliationID必须与条件匹配,或者条件值没有意义,因此所有记录的完整条件都为真。

答案 2 :(得分:0)

另一种选择:

var associations = (from a in ftjent.Associations
                join ap in ftjent.AssociationProducts on a.AssociationID equals   ap.AssociationID
                where ap.Product.Name == productName               
                select new
                    {
                        a.Acronym,
                        a.AssociationID,
                        a.Name,
                        a.AffiliationID //add AffilationID into the unknown type

                     }
                 ).Distinct().OrderBy(assoc => assoc.Acronym);

if(affilationID != 0)
     associations = associations.Where(a=>a.AffiliationID == affiliationID);

这不是最短的代码,但它清楚地表明了流程的分离。

选择就是你。

答案 3 :(得分:-1)

使用查询理解语法很难做到这一点,但是在使用查询理解语法进行查询之前,您总是可以提取逻辑并创建另一个IQueryable。

var products = ftjent.AssociationProducts;
if (affiliationID != null)
    products = products.Where(x => x.AffiliationID = affiliationID);

var associations = (
    from a in ftjent.Associations
    join ap in products on a.AssociationID equals ap.AssociationID
    where ap.Product.Name == productName
    select new
    {
        a.Acronym,
        a.AssociationID,
        a.Name
    }
)
.Distinct()
.OrderBy(assoc => assoc.Acronym);