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

本文关键字:guid 比较 linq 查询 | 更新日期: 2023-09-27 17:49:26

当这个尝试运行时,我得到一个错误:

 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不识别该方法"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]]]反向(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]]])"方法,并且无法将此方法转换为存储表达式。之前

我不认为转换为可空的guid在这里工作。c.CorporationId是一个可空的guid,但p.corporationid只是一个guid。

有什么建议吗?

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

您是否尝试过放弃cast并将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
              };

似乎在抱怨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
           };

这是一个老问题,但由于没有答案,这里是要点。在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
                   };

但是你必须问这个问题,如果你必须转换这个,为什么这个模型有这个列被标识为不可空(如果它被设置正确,你可能不会面临在这一点上必须转换)。