2012-12-21 26 views
4

我得到一個錯誤,當這種嘗試運行:如何將一個可空的guid與一個linq查詢中的guid進行比較?

Nullable<Guid> ng = corpid; 

    var qry1 = from c in entities.Transactions 
      join p in entities.Products on c.CorporationId equals (Nullable<Guid>) p.CorporationId 
       where c.Branch == br && 
        c.AccountNumber == accountnumber && 
        c.CorporationId == ng 
       orderby c.TransactionDate descending 
       select new 
       { 
       Date = c.TransactionDate, 
       RefNo = c.ReferenceNumber, 
       DlvryAcct = c.DeliveryAccount, 
       Desc = p.Description, 
       GasQty = c.GasQuantity, 
       Total = c.Amount, 
       Balance = c.Balance 
       }; 

這是消息:

 
LINQ to Entities does not recognize the method 
'System.Linq.IQueryable`1[f__AnonymousType1`7[System.Nullable`1[System.DateTime], 
    System.String,System.String,System.String,System.Nullable`1[System.Decimal], 
    System.Nullable`1[System.Decimal],System.Nullable`1[System.Decimal]]] 
Reverse[f__AnonymousType1`7](System.Linq.IQueryable`1[f__AnonymousType1`7[System.Nullable`1[System.DateTime], 
    System.String,System.String,System.String,System.Nullable`1[System.Decimal], 
    System.Nullable`1[System.Decimal],System.Nullable`1[System.Decimal]]])' 
method, and this method cannot be translated into a store expression. 

我不認爲中投可空GUID是在這裏工作。 c.CorporationId是一個可以爲空的指導,但p.corporationid只是一個指導。

有什麼建議嗎?

+0

你能這樣做嗎? 在實體中連接p。在c.CorporationId上的產品等於p.CorporationId == null ?? EMPTY_GUID:p.CorporationId –

+0

如果您簡單地比較指令 'c.CorporationId等於p.CorporationId'或'c.CorporationId.GetValueOrDefault()等於p.CorporationId' –

回答

3

您是否嘗試過前述的鑄造和等同c.CorporationIdng.Value代替?:

Nullable<Guid> ng = corpid; 

    var qry1 = from c in entities.Transactions 
      join p in entities.Products on c.CorporationId equals p.CorporationId 
       where c.Branch == br && 
        c.AccountNumber == accountnumber && 
        c.CorporationId == ng.Value 
       orderby c.TransactionDate descending 
       select new 
       { 
       Date = c.TransactionDate, 
       RefNo = c.ReferenceNumber, 
       DlvryAcct = c.DeliveryAccount, 
       Desc = p.Description, 
       GasQty = c.GasQuantity, 
       Total = c.Amount, 
       Balance = c.Balance 
       }; 
+0

我發生了什麼,但它沒有做出任何區別。我刪除了連接,ng.value仍然出錯。因此,我刪除了ng.value比較,並且由於可以爲空的guid導致加入。 – ErocM

0

這似乎是在抱怨SELECT子句中的匿名構造函數。 c.TransactioDate,c.GasQuantity,c.Amountc.Balance都顯示爲可空。嘗試這樣的事情,看看這些領域是否是問題。

Nullable<Guid> ng = corpid; 

var qry1 = from c in entities.Transactions 
      join p in entities.Products on c.CorporationId equals (Nullable<Guid>) p.CorporationId 
      where c.Branch == br && 
       c.AccountNumber == accountnumber && 
       c.CorporationId == ng.Value 
      orderby c.TransactionDate descending 
      select new 
      { 
       Date = c.TransactionDate.Value, 
       RefNo = c.ReferenceNumber, 
       DlvryAcct = c.DeliveryAccount, 
       Desc = p.Description, 
       GasQty = c.GasQuantity.Value, 
       Total = c.Amount.Value, 
       Balance = c.Balance.Value 
      }; 
0

這是一個古老的問題,但由於沒有答案,所以這裏就是骨感。在C#中GUID是一個非空的對象,所以你永遠不能映射爲null的Guid,但你可以映射NULL來的Guid?所以這裏的解決方案:

var qry1 = from c in entities.Transactions 
       join p in entities.Products on c.CorporationId equals p.CorporationId 
      where c.Branch == branch 
       && c.AccountNumber == accountNumber 
       && ((Guid?)c.CorporationId).Value == null // This is the secret sauce 
      orderby c.TransactionDate descending 
      select new 
        { 
         Date = c.TransactionDate, 
         RefNo = c.ReferenceNumber, 
         DlvryAcct = c.DeliveryAccount, 
         Desc = p.Description, 
         GasQty = c.GasQuantity, 
         Total = c.Amount, 
         Balance = c.Balance 
        }; 

不過,我可能會做這樣的:

var qry1 = from c in entities.Transactions.Where(t => ((Guid?)t.CorporationId).Value == null) 
       join p in entities.Products on c.CorporationId equals p.CorporationId 
      where c.Branch == branch 
       && c.AccountNumber == accountNumber 
      orderby c.TransactionDate descending 
      select new 
        { 
         Date = c.TransactionDate, 
         RefNo = c.ReferenceNumber, 
         DlvryAcct = c.DeliveryAccount, 
         Desc = p.Description, 
         GasQty = c.GasQuantity, 
         Total = c.Amount, 
         Balance = c.Balance 
        }; 

但是你要問,如果你有,爲什麼模型此列確定施放此爲不可爲空的(如果設置正確,你可能不會面臨不得不投的問題在此刻)。

相關問題