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]]]
反向[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 的强制转换在这里不起作用。Thec.CorporationId是一个可为空的 guid,但 thep.corporationid只是一个 guid。

有什么建议么?

4

3 回答 3

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
              };
于 2012-12-21T20:04:17.200 回答
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
           };
于 2012-12-21T20:26:43.177 回答
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
                   };

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

于 2017-09-20T01:54:59.163 回答