【问题标题】:How to do Group Concat in System.Linq.Dynamic如何在 System.Linq.Dynamic 中进行 Group Concat
【发布时间】:2017-09-26 21:37:45
【问题描述】:

我正在尝试在动态 linq 中连接字符串,即在常规 linq 中像 string.Join(",", g.Select(i => i.item1)) 一样

我的动态 linq 看起来像这样

result.AsEnumerable().AsQueryable().GroupBy("new {it.id.ToString() as entity_id, it[\"item2\"] as item2}", "it").Select("new{key.entity_id, key.item2,
String.Join(\", \", it.Select(it[\"item1\"].ToString())) as item1}")

但它显然不起作用,任何想法 我只想要每个组的逗号分隔字符串

编辑添加的数据示例:

只是为了说明数据问题 它的样子:

Current State

注意:对于 PK 1,item1 重复两次,因为它来自 N:N 关系,我需要在 pk 上对它进行 Group Concat

Required State

已解决

我找到了解决此问题的方法,在我的情况下,我需要根据 pil0t 的How to implement SelectMany in System.Linq.Dynamic ExpressionParser 向 System.Linq.Dynamic 添加更多功能 根据您需要更改的答案

1) 签名接口IEnumerableSignatures 并添加到它:

void Select(string selector); void SelectMany(string selector);/*not needed for this but good to have*/

2) 根据他/她的回答修改 ParseAggregate 函数以添加

.... if (signature.Name == "Min" || signature.Name == "Max") { typeArgs = new Type[] { elementType, args[0].Type }; } else if (signature.Name == "Select") { typeArgs = new Type[] { elementType, Expression.Lambda(args[0],innerIt).Body.Type}; } else if (signature.Name == "SelectMany") { var type = Expression.Lambda(args[0], innerIt).Body.Type; var interfaces = type.GetInterfaces().Union(new[] { type }); Type resultType = interfaces.Single(a => a.Name == typeof(IEnumerable<>).Name).GetGenericArguments()[0]; typeArgs = new Type[] { elementType, resultType }; } ....

3) 重新编译添加到项目中

4) 使用如下:String.Join(\", \", Select(it[\"item1\"].ToString())

【问题讨论】:

    标签: c# dynamic-linq


    【解决方案1】:

    如果你使用System.Linq.Dynamic,你可以在你的Select new中连接

    Select("new(name,courseID, (name + courseID.ToString()) as y)")
    

    下面有一个完整的控制台应用程序,因此您可以测试整个代码

    class Program
        {
            static void Main(string[] args)
            {
    
                List<course> Courses = new List<course>();
                Courses.Add(new course() { name = "CA", courseID = 1 });
                Courses.Add(new course() { name = "CB", courseID = 2 });
                Courses.Add(new course() { name = "CC", courseID = 3 });
    
                string column_name = "name";
                string column_value = "C";
                string where = string.Format("{0}.Contains(@0)", column_name); 
                var result = Courses.Where(where, column_value).Select("new(name,courseID, (name + courseID.ToString()) as y)").Take(50);   
                foreach (var item in result)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
        }
        public class course
        {
            public string name { get; set; }
            public int courseID { get; set; }
        }
    

    【讨论】:

    • 感谢您的回复,但我正在搜索 group concat,我在上面添加了数据示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2012-06-29
    • 2021-06-03
    • 2020-06-04
    • 2011-06-03
    • 2016-03-06
    相关资源
    最近更新 更多