0

我有一个正在创建列表的 linq 查询。我只想使用第二个where子句 if 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

4 回答 4

3

与 0比较affiliationId,如果它没有值,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);
于 2012-08-24T18:31:44.833 回答
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);

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

选择是你的。

于 2012-08-24T18:34:58.080 回答
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 必须与条件匹配,要么条件值没有意义,因此所有记录的完整条件都为真。

于 2012-08-24T18:31:48.183 回答
-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);
于 2012-08-24T18:34:55.673 回答