【问题标题】:LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' why?LINQ to Entities 无法识别方法“Int32 Parse(System.String)”,为什么?
【发布时间】:2014-07-31 18:24:19
【问题描述】:

我有代码

 foreach (XElement t in selxml.Elements("Type"))
                printxml.Add(new XElement("Type",
                    new XAttribute("TypeID", t.Attribute("ID").Value),
                    new XAttribute("TypeName", (from pt in fdb.PrintTypes where pt.PrintTypeID == Int32.Parse(t.Attribute("ID").Value) select pt.Name).SingleOrDefault()),
                    new XAttribute("Value", t.Attribute("Value").Value),
                    new XAttribute("ValueName", (from pti in fdb.PrintTypeItems
                                                 where pti.Value == Int32.Parse(t.Attribute("Value").Value) &&
                                                       pti.PrintTypeID == Int32.Parse(t.Attribute("ID").Value)
                                                 select pti.Name).SingleOrDefault())));

和VS说

LINQ to Entities 无法识别方法 'Int32 Parse(System.String)' 方法,并且该方法无法翻译 到商店表达式中。

为什么选择 LINQ to Entity?这是 Linq 到 XML。我该如何解决这个问题?

【问题讨论】:

  • 是的,但是您有一个 L2E 嵌套查询,它不支持 Int32.Parse
  • @James h如何使使用 L2XML 嵌套 L2E 成为可能?

标签: c# xml entity-framework


【解决方案1】:

我建议您在 foreach 循环中调用 Add 方法之前使用 Int32.Parse 方法。您可以将两者保存为局部变量(值和 ID)并在同一个查询中使用它们。

【讨论】:

  • +1 实际上是个好地方,我没有注意到 OP 引用了 foreach 变量(太忙于查看 LINQ 的东西!)。
猜你喜欢
  • 2015-09-20
  • 2015-07-20
  • 2011-12-07
  • 2015-07-25
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
相关资源
最近更新 更多