How to do Group Concat in System.Linq.Dynamic

c# dynamic-linq

Question

In Dynamic linq, I'm attempting to concatenate strings, so something like thatstring.Join(",", g.Select(i => i.item1)) in standard linq

My dynamic linq seems 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 clearly it doesn't work; any suggestions? All I need is a string with commas between each group.

Edit Added several data examples:

only to demonstrate the data issue How it appears:

Present Situation

I need to Group Concatenate item1 on, let's say, PK 1 since it comes from a N:N connection and repeats twice.

Needed State

RESOLVED

I've figured out how to fix this problem; in my situation, I had to give the system extra capability. Linq.Dynamic, according to pil0the system's SelectMany implementation. Dynamic Expression Parser for Linq t's and Depending on the response, you must alter.

1) Add the following to the signatures interface IEnumerableSignatures:

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

2) change the ParseAggregate function in accordance with the response 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 }; } ....

Recompile and add to the project.

4) Employ the next: String. Join ("," "") and "select ("it["item1")"). ToString())

1
0
9/27/2017 2:52:12 PM

Popular Answer

You may use this method to concatenate your Choose new if you are using System.Linq.Dynamic.

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

A full console program is provided below so you can try the whole 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; }
    }
0
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