2012-12-21 24 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 
        }; 

但是你要问,如果你有,为什么模型此列确定施放此为不可为空的(如果设置正确,你可能不会面临不得不投的问题在此刻)。

相关问题