简体   繁体   English

如何在 System.Linq.Dynamic 中进行 Group Concat

[英]How to do Group Concat in System.Linq.Dynamic

I am trying to concatenate strings in Dynamic linq, ie smth like string.Join(",", g.Select(i => i.item1)) in regular linq我试图在动态 linq 中连接字符串,即像string.Join(",", g.Select(i => i.item1))在常规 linq 中那样

my dynamic linq looks something like this我的动态 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}")

but it obviously doesn't work, any ideas I just want a comma separated string for each of the groups但它显然不起作用,任何想法我只想为每个组使用逗号分隔的字符串

Edit Added examples of data:编辑添加的数据示例:

just to illustrate the data problem How it looks like:只是为了说明数据问题它的样子:

Current State当前状态

Note: item1 repeats twice for PK 1 because it comes from a N:N relationship,i need to Group Concat it on say pk注意:item1 对 PK 1 重复两次,因为它来自 N:N 关系,我需要在 pk 上对其进行分组

Required State所需状态

RESOLVED解决

I have found a way to resolve this issue, in my case i needed to add more functionality to System.Linq.Dynamic, as per How to implement SelectMany in System.Linq.Dynamic ExpressionParser by pil0t As per the answer you need to change我找到了解决这个问题的方法,在我的情况下,我需要向 System.Linq.Dynamic 添加更多功能,根据How to implement SelectMany in System.Linq.Dynamic ExpressionParser by pil0t根据您需要更改的答案

1) the signatures interface IEnumerableSignatures and add to it: 1) 签名接口IEnumerableSignatures并添加到它:

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

2) modify ParseAggregate function as per his/her answer to add 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) recompile add to project 3)重新编译添加到项目

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

if you are using System.Linq.Dynamic you can concat in your Select new this way如果您使用的是System.Linq.Dynamic ,则可以通过这种方式连接Select new

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

Below you have a complete console application so you can test the entire code下面是一个完整的控制台应用程序,因此您可以测试整个代码

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; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM