簡體   English   中英

Linq-to-SQL:忽略WHERE子句中的空參數

[英]Linq-to-SQL: Ignore null parameters from WHERE clause

下面的查詢應該返回在ownerGroupIds中提供匹配ID或匹配ownerUserId的記錄 但是ownerUserId為null,我希望忽略查詢的這一部分。

    public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds)
    {
        return ( from c in db.Contacts
                 where 
                 c.Active == true 
                 &&
                 c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
                 &&
                 ( // Owned by user
                    !ownerUserId.HasValue || 
                    c.OwnerUserId.Value == ownerUserId.Value
                 )
                 &&
                 ( // Owned by group
                    ownerGroupIds.Count == 0 ||
                    ownerGroupIds.Contains( c.OwnerGroupId.Value )
                 )
                 select c ).Count();
    }

但是,當為ownerUserId傳入null時,我收到以下錯誤: Nullable object must have a value.

在這種情況下,我可能不得不使用lambda表達式?

你的問題是你沒有傳入一個可以為空的int,你傳遞的是null。

試試這個:

Print(null);

private void Print(int? num)
{
     Console.WriteLine(num.Value);
}

你得到同樣的錯誤。

它應該工作如果你這樣做:

var q = ( from c in db.Contacts
                 where 
                 c.Active == true 
                 &&
                 c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
                 &&
                 ( // Owned by group
                    ownerGroupIds.Count == 0 ||
                    ownerGroupIds.Contains( c.OwnerGroupId.Value )
                 )
                 select c );

if(ownerUserId != null && ownerUserId.HasValue)
     q = q.Where(p => p.OwnerUserId.Value == ownerUserId.Value);

return q.Count();

你和OwnerUserId有一些聯系嗎? 如果是,則c.OwnerUserId可以為null,並且在c.OwnerUserId.Value沒有任何值

有條件地將where子句添加到表達式樹中怎么樣?

public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds)
    {

    var x = ( from c in db.Contacts
                 where 
                 c.Active == true 
                 &&
                 c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
                 &&
                 ( // Owned by group
                    ownerGroupIds.Count == 0 ||
                    ownerGroupIds.Contains( c.OwnerGroupId.Value )
                 )
                 select c );

    if (ownerUserId.HasValue) {
        x = from a in x
            where c.OwnerUserId.Value == ownerUserId.Value
    }

    return x.Count();
    }

問題:“&&”和“||” 被轉換為像“AndCondition(a,b)”這樣的方法,所以“!a.HasValue || a.Value == b”變為“OrCondition(!a.HasValue,a.Value == b);” 這樣做的原因可能是獲得適用於代碼和SQL語句的通用解決方案。 因此,請使用“?:”表示法。

有關更多信息,請參閱我的博客文章: http//peetbrits.wordpress.com/2008/10/18/linq-breaking-your-logic/

// New revised code.
public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds)
{
    return ( from c in db.Contacts
             where 
             c.Active == true 
             &&
             c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
             &&
             ( // Owned by user
                // !ownerUserId.HasValue || 
                // c.OwnerUserId.Value == ownerUserId.Value
                ownerUserId.HasValue ? c.OwnerUserId.Value == ownerUserId.Value : true
             )
             &&
             ( // Owned by group
                // ownerGroupIds.Count == 0 ||
                // ownerGroupIds.Contains( c.OwnerGroupId.Value )
                ownerGroupIds.Count != 0 ? ownerGroupIds.Contains( c.OwnerGroupId.Value ) : true
             )
             select c ).Count();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM