LINQ to SQL 表达式中的 DataContractSerializer?
我是 LINQ 表达式的新手,并试图让以下内容发挥作用:
public IQueryable<Models.Powerups.Powerup> GetPowerups(Guid userid)
{
return from p in dc.Powerups
where p.UserId == userid
select (Models.Powerups.Powerup) new DataContractSerializer(Type.GetType(p.Type, true)).ReadObject(new XmlTextReader(new StringReader(p.Data)));
}
它显然不起作用,因为 LINQ 试图将整个表达式转换为 SQL。
你知道有什么方法可以做到这一点吗?
I'm new to LINQ expressions and trying to get the following to work:
public IQueryable<Models.Powerups.Powerup> GetPowerups(Guid userid)
{
return from p in dc.Powerups
where p.UserId == userid
select (Models.Powerups.Powerup) new DataContractSerializer(Type.GetType(p.Type, true)).ReadObject(new XmlTextReader(new StringReader(p.Data)));
}
It obviously doesn't work since LINQ is trying to translate the entire expression to SQL.
Do you know a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的办法是这样做:
一旦从数据库取回 xml,就自己反序列化它。 LINQ to SQL 提供程序不会知道如何将其转换为 SQL,而且您也不希望它这样做。 无论如何,序列化对象的反序列化应该发生在 CLR 中。
The best thing would be to do this:
Then deserialize the xml yourself once you get it back from the database. The LINQ to SQL provider will not know how to turn this into SQL and you wouldn't want it to anyways. The deserialization of serialized objects ought to happen in the CLR anyhow.
您需要将其分为两部分,第一部分从数据库获取数据,然后在数据查询之外进行转换。 您可以使用 C# 中自动生成的迭代器来提供帮助:
这样做的好处是它还允许延迟执行。
You need to break this into two parts, the first to get the data from the database, and then the transformation outside of that query for the data. You can use auto-generated iterators in C# to help:
The great thing about this is that it allows for deferred execution as well.