2012-03-27 24 views
1

此空檢查是我的查詢:在LINQ

var entityMerchantVisit = 
    from e in context.MerchantCustomerVisit 
    where e.CustomerId == currentCustGuid 
    group e by 
      new { e.Merchant.Locations.FirstOrDefault().CityId } into mcvGroup 
    orderby mcvGroup.Count() descending 
    select mcvGroup; 

我收到提示

「演員陣容價值型‘的Int32’失敗,因爲物化值爲空」時ê .Merchant.Locations.FirstOrDefault()。CityId爲空。

如何檢查它是否爲空。如果它爲空,我想將它分配爲(int) 0

+3

僅供參考,在這裏被認爲是一種很好的形式來標記最有用的答案被接受。如果你的問題已經回答,請這樣做! – Ben 2012-04-04 01:52:09

回答

4

像這樣的東西可以工作:基於您的評論也許你可以試試下面的(注意括號)

var entityMerchantVisit = 
    from e in context.MerchantCustomerVisit 
    where e.CustomerId == currentCustGuid 
    group e by 
      new { e.Merchant.Locations.FirstorDefault() != null 
        ? e.Merchant.Locations.First().CityId : 0 
       } into mcvGroup 
    orderby mcvGroup.Count() descending 
    select mcvGroup; 

 group e by 
      new { CityID = ((int)e.Merchant.Locations.FirstorDefault() != null 
        ? e.Merchant.Locations.First().CityId : 0) 
       } into mcvGroup 
    orderby mcvGroup.Count() descending 
+0

'.First()'不會返回'null',如果沒有項目,它將拋出異常。您應該修改爲'.FirstOrDefault()'。 – 2012-03-27 04:12:00

+1

@AlastairPitts啊!哎呦,輸入這個太快了。固定。 – gideon 2012-03-27 04:15:49

+0

我試過 group e by new {e.Merchant.Locations.FirstOrDefault()。CityId == null? e.Merchant.Locations.FirstOrDefault()。CityId:0}轉換爲mcvGroup 但它表示無效的匿名類型成員聲明器,它是什麼意思? – user777310 2012-03-27 08:09:40

0

使用空合併運算符(? ?)

var entityMerchantVisit = 
from e in context.MerchantCustomerVisit 
where e.CustomerId == currentCustGuid 
group e by 
     new { (e.Merchant.Locations.FirstOrDefault().CityId ?? 0) } into mcvGroup 
orderby mcvGroup.Count() descending 
select mcvGroup; 
+0

這不會改變任何東西。 「NullReferenceException」的原因是OP正嘗試從'null'' Location'訪問'CityId'。你的代碼將拋出完全相同的異常。 – 2012-03-27 04:14:20

+0

你在哪裏看到一個NullReferenceException?他沒有得到例外。看到這個問題。 http://stackoverflow.com/questions/6864311/the-cast-to-value-type-in​​t32-failed-because-the-materialized-value-is-null。 – Alan 2012-03-27 04:18:51

+0

我的歉意。我不知道LINQ-to-SQL解釋器如何處理空合併運算符。我試圖刪除-1,但該死的東西不會讓我:( – 2012-03-27 04:21:35

0

你可以嘗試使用Nullable<int>表達:

var entityMerchantVisit = 
    from e in context.MerchantCustomerVisit 
    where e.CustomerId == currentCustGuid 
    group e by new { 
     CityId = e.Merchant.Locations.Any() ? 
      e.Merchant.Locations.First().CityId 
      : default(int?) 
    } into mcvGroup 
    orderby mcvGroup.Count() descending 
    select mcvGroup; 
+0

通過對類型inting的分組,可以保留從'Int32.MinValue'到'Int32.MaxValue'的'CityId'值的完整範圍。 – devgeezer 2012-03-27 04:49:55

4

可以使用let語法綁定e.Merchant.Locations.FirstOrDefault()的一系列變量,然後檢查了空。這可讓您方便地識別沒有位置的商家,併爲您提供簡潔的三元運算符表達式以便啓動。

var entityMerchantVisit = 
    from e in context.MerchantCustomerVisit 
    where e.CustomerId == currentCustGuid 
    let location = e.Merchant.Locations.FirstOrDefault() 
    group e by 
      new { CityId = (location == null ? 0 : location.CityId) } into mcvGroup 
    orderby mcvGroup.Count() descending 
    select mcvGroup; 
+0

很好的使用'let'並記住將'CityId'屬性名稱添加到匿名類型中。 +1 – devgeezer 2012-03-27 04:52:11