如何在linq查询中比较可空的guid与guid?

时间:2012-12-21 19:59:40

标签: c# 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是一个可以为空的guid,但p.corporationid只是一个guid。

有什么建议吗?

3 个答案:

答案 0 :(得分:3)

您是否尝试过前面的演员并将c.CorporationId等同于ng.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
              };

答案 1 :(得分:0)

似乎在抱怨select子句中的匿名构造函数。 c.TransactioDatec.GasQuantityc.Amountc.Balance似乎都是Nullable。尝试这样的事情只是为了看看这些字段是否是问题。

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
           };

答案 2 :(得分:0)

这是一个老问题,但由于没有答案,这里是瘦的。在C#中Guid是一个不可为空的对象,所以你不能将null映射到Guid,但你可以将Null映射到Guid ?,所以这是解决方案:

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
                   };

但是,我可能会这样做:

{{1}}

但你必须提出一个问题,如果你不得不抛出这个为什么模型将此列标识为不可为空(如果设置正确,你可能不会面临此时必须抛出)