How to do Group Concat in System.Linq.Dynamic

c# dynamic-linq


I am trying to concatenate strings in Dynamic linq, i.e. smth like string.Join(",", g.Select(i => i.item1)) in regular linq

my dynamic linq looks something like this

result.AsEnumerable().AsQueryable().GroupBy("new { 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

Required State


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

1) the signatures interface IEnumerableSignatures and add to it:

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

.... 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

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

9/27/2017 2:52:12 PM

Popular Answer

if you are using System.Linq.Dynamic you can concat in your Select new this way

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)
    public class course
        public string name { get; set; }
        public int courseID { get; set; }
9/26/2017 10:14:17 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow