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 {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:
Note: item1 repeats twice for PK 1 because it comes from a N:N relationship,i need to Group Concat it on say pk
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
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())