0

I need to join List of string into string inside linq select. I tried that:

var myEnt = from p in ctx.Project
    select new ProjectRepository.Project
    {
        Id = p.ProjectId,
        Desc = p.ProjectDesc,
        UsersProject = String.Join("; ", (
                                from up in ctx.usersProject join u in ctx.users
                                on up.user equals u.id into uloj from uj in uloj.DefaultIfEmpty()
                                where (up.deleted ?? false) == false
                                && up.projectId == p.Id
                                && (uj.deleted ?? false) == false
                                select uj.name + " " + uj.surname).ToList())
});

gridProg.DataSource = myEnt.ToList();
gridProg.DataBind();

But i had this error:

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.

Thank you.

UPDATE

New error after adding .Tolist() to DataSource binding.

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.

frasca
  • 31
  • 1
  • 1
  • 6
  • 3
    Possible duplicate of [Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported](https://stackoverflow.com/questions/12938371/data-binding-directly-to-a-store-query-dbset-dbquery-dbsqlquery-is-not-suppo) – Sushant Yelpale Nov 25 '19 at 09:44
  • From the error message : "Data **binding directly** to a store query .. **is not supported**. **Instead populate a DbSet with data**, .. **For WinForms** .. **ToBindingList()** .. **For ASP.NET** .. **ToList()**" Add a `ToList` to the UsersProject – xdtTransform Nov 25 '19 at 09:46
  • @SushantYelpale I add .ToList() in datasource of my grid. And now i had new error. I post it in the question. Maybe i cannot use String.Join inside Linq.. – frasca Nov 25 '19 at 09:49
  • where you have added `.ToList()` ? Edit the code – Sushant Yelpale Nov 25 '19 at 09:50
  • `grid.DataSource = myEnt.ToList(); gridProg.DataBind();` – frasca Nov 25 '19 at 09:51
  • Compute `string.Join` outside, then add it to `ProjectRepository.Project`. – Sushant Yelpale Nov 25 '19 at 09:52
  • ok i try. Thank you – frasca Nov 25 '19 at 09:54
  • May [this answer](https://stackoverflow.com/a/9647063/1509853) can help ? or [here](http://andreyzavadskiy.com/2016/04/21/querying-entity-model-part-10-group-concatenation/) =) – oCcSking Nov 25 '19 at 10:11

2 Answers2

1

I have not tested it, but it will work. Make 2 different Queries

var Projects = (from up in ctx.usersProject join u in ctx.users
    on up.user equals u.id into uloj from uj in uloj.DefaultIfEmpty()
    where (up.deleted ?? false) == false
        && up.projectId == p.Id
        && (uj.deleted ?? false) == false
        select new { 
            ProjectId = up.projectId, 
            ProjectsList = uj.name + " " + uj.surname
        }).ToList();

var myEnt = from p in ctx.Project.AsEnumerable()
    select new ProjectRepository.Project
    {
        Id = p.ProjectId,
        Desc = p.ProjectDesc,
        UsersProject = String.Join("; ", Projects.Where(e=> p.ProjectId == e.ProjectId).Select(e=> e.ProjectsList).ToList())
    }).ToList();
Sushant Yelpale
  • 860
  • 6
  • 19
  • Doesn't work. Got the 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.** – frasca Nov 25 '19 at 10:00
  • Maybe i can update the value after binding var MyEnt – frasca Nov 25 '19 at 10:02
  • Hi Sushant. Get the same 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.** Maybe the problem is String Join. maybe i have to find another way for concat list.. – frasca Nov 25 '19 at 10:20
  • OK OK. I add The AsEnumerable() to ctx.Project. – frasca Nov 25 '19 at 10:22
  • **The nullable object must have a value.** – frasca Nov 25 '19 at 10:23
0

I found the solution. I post the code below:

var usersProject = (from up in ctx.usersProject join u in ctx.users
                on up.user equals u.id into uloj from uj in uloj.DefaultIfEmpty()
                where (up.deleted ?? false) == false
                && up.projectId == p.Id
                && (uj.deleted ?? false) == false
                select new { 
                   ProjectId = up.projectId, 
                   User = uj.name + " " + uj.surname
                }).ToList();

var myEnt = from p in ctx.Project.AsEnumerable()
            select new ProjectRepository.Project
            {
               Id = p.ProjectId,
               Desc = p.ProjectDesc
            }).ToList();

 var myEntL = myEnt.ToList();
 foreach (var mysingleEnt in myEntL)
 {
    myEntL.Where(x => x.Id == mysingleEnt.Id).FirstOrDefault().utentiAssociati =
    String.Join("; ", usersProject
       .Where(x => x.ProjectId == mysingleEnt.Id).Select(x => x.User).ToList());
  }

  gridProg.DataSource = myEntL;
  gridProg.DataBind();

Thank you for help.

frasca
  • 31
  • 1
  • 1
  • 6