How do I concatenate strings in Entity Framework Query? How do I concatenate strings in Entity Framework Query? sql sql

How do I concatenate strings in Entity Framework Query?


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)                      });


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;            }