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

4

当我尝试运行这个程序时,出现了一个错误:

 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 不识别方法 '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]]])',
而且这个方法不能被转换成一个存储表达式。
我不认为在这里将c.CorporationId强制转换为可空guid是有效的,因为 c.CorporationId 是一个可空 guid,但是p.corporationid只是一个普通的 guid。
有什么建议吗?


你能像这样做吗?在entities.Products中加入p,当c.CorporationId等于null时,等于p.CorporationId == EMPTY_GUID。 - Andrew Walters
如果您仅比较GUID(全局唯一标识符),会发生什么呢?c.CorporationId equals p.CorporationId 或者 c.CorporationId.GetValueOrDefault() equals p.CorporationId - AD.Net
3个回答

3

你尝试过放弃使用 casting,而是将 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
              };

我尝试过了,但没有任何改变。我删除了连接(join)和ng.value仍然出错。因此,我删除了ng.value比较,连接(join)由于可空guid而失败。 - ErocM

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

但是你必须问自己这个问题,如果你不得不进行强制转换,为什么模型将此列标识为不可为空(如果设置正确,你可能不会面临在此时进行强制转换的情况)。


0

它似乎在选择子句中抱怨匿名构造函数。 c.TransactioDatec.GasQuantityc.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
           };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接