gpt4 book ai didi

sql - 如何在 Entity Framework 查询中连接字符串?

转载 作者:行者123 更新时间:2023-12-03 22:29:17 25 4
gpt4 key购买 nike

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

  • 苹果
  • 香蕉
  • 草莓

  • 如果我喜欢这个

    var dataSource = this.context
    .农场
    .Select(f => 新
    {
    f.Id,
    Fruits = string.Join(", ", f.Fruits)
    });

    当然我会收到这个错误

    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.


    有什么解决办法吗?

    最佳答案

    您必须在投影之前执行查询。否则 EF 会尝试翻译 Join方法进入SQL (显然失败了)。

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

    关于sql - 如何在 Entity Framework 查询中连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4095658/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com