c# - 如何在 Linq toEntity 中将字符串数组转换为字符串?

标签 c# string linq entity-framework linq-to-entities

我有下面的 linq 查询

var dd = (from dt in d
          select new
          {
              dt.id,
              dt.date,
              dt.customerid,
              dt.customer,
              dt.Description,
              dt.defect,
              dt.address,
              products = string.Join(",", dt.products)
          }).ToList();

string.Join 不起作用,错误是

LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'

我用 Google 搜索了很多,但找不到任何其他解决方案。我想做的是,dt.Products 是一个数组,我想将它们全部连接到一个字符串中。在 Linq to Entities 中可能吗?

最佳答案

您无法在查询中执行此操作,但可以通过调用 .AsEnumerable() 在内存中执行此操作。第一的。试试这个:

var dd = 
    (from dt in d
     select new
     {
         dt.id,
         dt.date,
         dt.customerid,
         dt.customer,
         dt.Description,
         dt.defect,
         dt.address,
         dt.products
     })
    .AsEnumerable()
    .Select(dt => new {
         dt.id,
         dt.date,
         dt.customerid,
         dt.customer,
         dt.Description,
         dt.defect,
         dt.address,
         products = string.Join(",", dt.products)
    }).ToList();

但请注意,如果您的数据集特别大,这可能会导致明显的性能影响。

关于c# - 如何在 Linq toEntity 中将字符串数组转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20004380/

相关文章:

c# - 如何将非托管 C++ dll 添加到 Silverlight 5 项目中

C# 文件 I/O 效率

c# - Hangfire:该类型不包含带有签名的方法

python - 为什么 '\x' 在 Python 中无效?

C - 将字符串数组分配给包含 21 个或更多崩溃的数组

c# - 如何使用 LINQ 实现此目的?

c# - .ToList().First() 与 .Single for IQueryable 在 c# 中的优势

c# - Hook 到 Quartz.net 中的关闭方法

string - GHC 接受的 unicode 字符范围

c# - LINQ:按时间分组的事件