【问题标题】:Join list into string将列表加入字符串
【发布时间】:2019-11-25 09:40:49
【问题描述】:

我需要将字符串列表加入到 linq 选择中的字符串中。 我试过了:

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

但我有这个错误:

不支持直接将数据绑定到存储查询(DbSet、DbQuery、DbSqlQuery、DbRawSqlQuery)。而是使用数据填充 DbSet,例如通过在 DbSet 上调用 Load,然后绑定到本地数据。对于 WPF 绑定到 DbSet.Local。对于 WinForms 绑定到 DbSet.Local.ToBindingList()。对于 ASP.NET WebForms,您可以绑定到在查询上调用 ToList() 的结果或使用模型绑定,有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=389592

谢谢。

更新

将 .Tolist() 添加到 DataSource 绑定后出现新错误。

LINQ to Entities 无法识别方法 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' 方法,并且此方法无法转换为存储表达式。

【问题讨论】:

  • 来自错误消息:“数据直接绑定到存储查询.. 不支持而是用数据填充 DbSet , .. 对于 WinForms .. ToBindingList() .. 对于 ASP.NET .. ToList()" 将ToList 添加到UsersProject
  • @SushantYelpale 我在网格的数据源中添加了 .ToList() 。现在我有新的错误。我将其发布在问题中。也许我不能在 Linq 中使用 String.Join ..
  • 您在哪里添加了.ToList()?编辑代码
  • grid.DataSource = myEnt.ToList(); gridProg.DataBind();

标签: c# linq


【解决方案1】:

我还没有测试过,但它会工作。进行 2 个不同的查询

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

【讨论】:

  • 不起作用。得到错误 ** LINQ to Entities 无法识别方法 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' 方法,并且此方法无法转换为存储表达式。**
  • 也许我可以在绑定 var MyEnt 后更新值
  • 嗨,苏珊特。得到同样的错误 LINQ to Entities does not identify the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' 方法,并且这个方法不能翻译成一个商店表达式。 也许问题是字符串连接。也许我必须为 concat list 找到另一种方式..
  • 好的好的。我将 AsEnumerable() 添加到 ctx.Project。
  • 可为空的对象必须有值。
【解决方案2】:

我找到了解决方案。我在下面发布代码:

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

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2017-10-29
    • 2010-09-12
    • 1970-01-01
    相关资源
    最近更新 更多