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

标签 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 是一个可为 null 的 guid,而 p.corporationid 只是一个 guid。

有什么建议吗?

最佳答案

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

关于c# - 如何在 linq 查询中将可为空的 guid 与 guid 进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13996433/

相关文章:

c# - 获取错误使用Timer类时跨线程操作

c# - 在 snmptrap.exe 运行时捕获 SNMP 陷阱

c# - 在结构上隐藏无参数构造函数

sql-server - 将所有 PK 和 FK GUID 列和关系转换为 int 的最快方法是什么?

c# - CrystalReport 和/或 Visual studio 2010 的情况很奇怪我不知道 .Net Framework

c# - 在 Linq 谓词中,编译器会优化对 Enumerable.Min() 的 "scalar"调用还是会为每个项目调用它?

c# - LINQ 或 lambda 表达式中的比较字符串之间的子句等效于什么?

c# - 使用 LINQ 过滤字符串

c - 如何在 C 中生成 GUID?

c# - 是否可以创建相同的 guid