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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4095658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:08:31  来源:igfitidea点击:

How do I concatenate strings in Entity Framework Query?

sqlentity-frameworkentity-framework-4aggregatestring-concatenation

提问by Ahmed Magdy

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 Fruitand Farmswith the following values:

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

  • 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.
LINQ to Entities 无法识别方法 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' 方法,并且此方法无法转换为存储表达式。

Is there any solution to this?

有什么解决办法吗?

采纳答案by Yakimych

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

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

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

回答by PCPGMR

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

接受@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;
            }