【发布时间】:2019-06-13 11:58:13
【问题描述】:
我在 XML 类型的数据库列中有一个 XML 结构,看起来像这样:
<Translation>
<Language ID="1">
<Title>Some title</Title>
<Abbrevation>some abbrevation</Abbrevation>
<Source>https://www.somesource.com</Source>
</Language>
<Language ID="2">
<Title>some more titles</Title>
<Abbrevation>some more abbrevation</Abbrevation>
<Source>https://www.somemoresource.com</Source>
</Sprache>
</Translation>
现在我需要将此 XML 的元素放入 LINQ to Entities 查询中。 到目前为止我尝试了什么:
return from x in _context.Test
where((displayAll == false && (x.Aufhebung == null || x.Aufhebung > DateTime.Now)) || (displayAll))
select new TestViewModel()
{
ID = x.ID,
Prop = x.Prop,
Number = x.Number ?? "",
Title = XElement.Parse(x.Translation).Descendants("Language").Where(f => f.FirstAttribute.Value == "1").Descendants("Title").Single().Value,
};
}
模型上的属性是:
public string Translation{ get; set; }
但我总是收到错误消息:
LINQ to Entities 无法识别方法 [System.Xml.Linq.XElement] System.Xml.Linq.XName,并且此方法无法转换为存储表达式。
有没有办法在 LINQ to Entities 中获取 LINQ to XML 子查询?
【问题讨论】:
标签: c# linq-to-entities linq-to-xml