在DataTable上对GroupBy使用动态LINQ [英] Using dynamic LINQ for GroupBy on DataTable

查看:312
本文介绍了在DataTable上对GroupBy使用动态LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多方法可以使动态LINQ用于.GroupBy,但是我看到的每个方法似乎都希望使用一个硬编码的实体.我想将以下内容复制为动态LINQ:

I have seen quite a few ways to get dynamic LINQ to work for the .GroupBy, but every one that I see seems to expect a hard coded entity. I want to reproduce the following as dynamic LINQ:

//need dynamic LINQ for this
var groupedRows = rows.GroupBy(z => new { make = z["make"], model = z["model"] })
                      .Select(x => x.Key);

我希望能够做到这一点,使整个函数成为一个字符串:

I'd like to be able to just do this, making the entire function a string:

var groupedRows = rows.GroupBy(z => "new { make = z[\"make\"], model = z[\"model\"] }")

我意识到,如果它只是一个常规实体,我可以做到

I realize that if it were only a regular entity, I could do this

mylist.GroupBy(z => new { make = z.make, model = z.model }).Select(x => x.Key);

如果我拥有该常规实体,则可以使用 Mitsu的动态GroupByMany .

If I had that regular entity, I could use Mitsu's dynamic GroupByMany.

我正在尝试使其与常规数据表(非强类型)一起使用.有任何想法吗?

I am trying to get this to work with a regular datatable (not strongly typed). Any ideas?

推荐答案

好,我能够使用Mono编译器即服务(MCS)在运行时同时使用反射代码和编译代码.利用问题注入变量进入Mono.CSharp.Evaluator(运行时从字符串中编译LINQ查询),我想出了以下解决方案(该问题的答案中的第二个):

Ok, I was able to get this working with both reflection and compiling code at runtime using the Mono Compiler as a Service (MCS). Utilizing the question Injecting a variable into the Mono.CSharp.Evaluator (runtime compiling a LINQ query from string), I came up with the following solution (#2 in the answer to that question):

DataTable dt = GetData();
         var dataRows = dt.AsEnumerable();

         //Initializing the evaluator  
         Evaluator.Init(new string[0]); 
          Evaluator.ReferenceAssembly(Assembly.GetAssembly(typeof(DataRow)));
          Evaluator.ReferenceAssembly(Assembly.GetExecutingAssembly());

         Evaluator.Run(@"using System;  
           using System.Linq;  
           using System.Collections.Generic;
           using System.Data;
           using MyNamespace;");

         var func = (Func<DataRow, object>)Evaluator.Evaluate(@"new Func<DataRow,object>(z => new
                {
                    make = z[""make""],
                    model = z[""model""]
                });");

         dataRows.GroupBy(func).Select(x => x.Key);

这样可以在运行时编译函数,然后在GroupBy中为每一行调用该函数.当我需要make或model的值时,然后需要使用反射通过我的GetPropertyValue扩展方法来获取该值.

So that compiles the function at runtime, then calls the function in the GroupBy for each row. When I needed the value of make or model, I then needed to use reflection to get to that value through my GetPropertyValue extension method.

即使遵循 Mono,我仍然无法在MCS中使用LINQ.编译即服务(MCS)问题,但是仅使用它来创建函数就可以完成我需要做的事情.

I still can't get LINQ working within the MCS, even after following the Mono Compiler as a Service (MCS) question, but just using it to creating the function did what I needed it to do.

这篇关于在DataTable上对GroupBy使用动态LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆