如何在实体框架查询中连接字符串?

发布于 2024-09-30 17:48:36 字数 609 浏览 5 评论 0原文

如何在实体框架 4 中连接字符串我有一个列中的数据,我想将逗号分隔的字符串另存为字符串,例如“value1,value2,value3” 在 EF4 中是否有方法或运算符可以执行此操作? 示例:假设我有两列 FruitFarms,其值如下:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_畞蕅 2024-10-07 17:48:36

您必须在投影之前执行查询。否则,EF 会尝试将 Join 方法转换为 SQL(显然会失败)。

var results = this.context
                  .Farms
                  .ToList()
                  .Select(f => new
                      {
                          f.Id, 
                          Fruits = string.Join(", ", f.Fruits)
                      });

You have to execute the query before projecting. Otherwise EF tries to translate the Join method into SQL (and obviously fails).

var results = this.context
                  .Farms
                  .ToList()
                  .Select(f => new
                      {
                          f.Id, 
                          Fruits = string.Join(", ", f.Fruits)
                      });
随风而去 2024-10-07 17:48:36

接受了@Yakimych 的回答,并认为如果有人需要的话可以提供我的答案:

using (myDBEntities db = new myDBEntities())
            {
                var results = db.Table
                    .ToList()
                    .Where(x => x.LastName.StartsWith("K"))
                    .Select(
                    x => new
                    {
                        x.ID,
                        Name = x.LastName + ", " + x.FirstName
                    }
                    );

                lstCoaches.DataValueField = "ID";
                lstCoaches.DataTextField = "Name";
                lstCoaches.DataSource = results;
                lstCoaches.DataBind();
                ListItem item = new ListItem
                {
                    Value = "0",
                    Text = "-- Make a Selection --"
                };
                lstCoaches.Items.Insert(0,item);
                lstCoaches.SelectedIndex = 0;
            }

Took @Yakimych answer and thought would provide mine if someone needed:

using (myDBEntities db = new myDBEntities())
            {
                var results = db.Table
                    .ToList()
                    .Where(x => x.LastName.StartsWith("K"))
                    .Select(
                    x => new
                    {
                        x.ID,
                        Name = x.LastName + ", " + x.FirstName
                    }
                    );

                lstCoaches.DataValueField = "ID";
                lstCoaches.DataTextField = "Name";
                lstCoaches.DataSource = results;
                lstCoaches.DataBind();
                ListItem item = new ListItem
                {
                    Value = "0",
                    Text = "-- Make a Selection --"
                };
                lstCoaches.Items.Insert(0,item);
                lstCoaches.SelectedIndex = 0;
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文