如何在实体框架查询中连接字符串?
如何在实体框架 4 中连接字符串我有一个列中的数据,我想将逗号分隔的字符串另存为字符串,例如“value1,value2,value3” 在 EF4 中是否有方法或运算符可以执行此操作? 示例:假设我有两列 Fruit
和 Farms
,其值如下:
- Apples
- Bananas
- Strawberry
如果我这样做,
var dataSource = this.context .Farms .Select(f => new { f.Id, Fruits = string.Join(", ", f.Fruits) });
我肯定会收到此错误
LINQ to Entities 无法识别“System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])”方法,并且此方法无法转换为存储表达式。
有什么办法解决这个问题吗?
How do I concatenate strings in Entity Framework 4 I have a data from a column and I want to save as a string a comma separated string like "value1, value2, value3"
Is there a method or an operator do do this in EF4?
Example: lets say that I have two columns Fruit
and Farms
with the following values:
- Apples
- Bananas
- Strawberries
If I do like this
var dataSource = this.context .Farms .Select(f => new { f.Id, Fruits = string.Join(", ", f.Fruits) });
Sure I will get this error
LINQ to Entities does not recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' method, and this method cannot be translated into a store expression.
Is there any solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在投影之前执行查询。否则,EF 会尝试将
Join
方法转换为SQL
(显然会失败)。You have to execute the query before projecting. Otherwise EF tries to translate the
Join
method intoSQL
(and obviously fails).接受了@Yakimych 的回答,并认为如果有人需要的话可以提供我的答案:
Took @Yakimych answer and thought would provide mine if someone needed: