System.Linq.Dynamic - 如何将Linq转换为动态语法

时间:2013-03-05 05:37:55

标签: c# visual-studio-2010 linq dynamic

我正在使用名为System.Linq.Dynamic(http://msdn.microsoft.com/en-us/vstudio//bb894665.aspx)的类。它用于定义动态Linq查询。我需要我的查询是动态的,因此可以在运行时定义它们。

我将用一个例子来解释: 有一个Transactions对象包含一个Transaction对象列表。

public class Transactions : List<Transaction>
{
    public Transactions() { }
    public Transactions(List<Transaction> trans) : base(trans) { }
}
public class Transaction
{
    public string Type;
    public int Value;
}

假设我声明了Transactions对象并用一些数据填充它:

Transactions trans = new Transactions
{
    new Transaction {
        Type = "A",
        Value = 20,
    },
    new Transaction {
        Type = "B",
        Value = 34,
    },
    ... and so on
};

然后我可以像这样运行Linq查询从trans:

中提取一些数据
var mySum = from tran in trans
            group tran by tran.Type into tranGroup
            select new
            {
                Type = tranGroup.Key,
                Total = tranGroup.Sum(s => s.Value)
            };

可以(不那么容易)转录为以下动态查询

var mySum = trans
    .AsQueryable()
    .GroupBy("new(Type)", "new(Value)")
    .Select("new(Key.Type as Type, Sum(Value) as Total)");

这样做的目的是给一个对象,其中Type为分组类型,Total为特定组的Summed Value。例如,它可能如下所示:

mySum = [{Type:"A", Total: 120},{Type:"B", Total: 200},{Type:"C", Total: 60}]

我理解动态查询的工作原理,但似乎没有任何明确的动态查询示例,其中包含对正在发生的事情的解释。

这是我无法解决的问题。鉴于以下Linq查询:

var myQuery = from tran in trans
              group tran by tran.Type into tranGroup
              select new
              {
                  Type = (tranGroup.Key == "A" ? "Alfa" : (tranGroup.Key == "B" ? "Bravo" : (tranGroup.Key == "C" ? "Charlie" : (tranGroup.Key == "D" ? "Delta" : "")))),
                  Data = (from tranValue in tranGroup
                          select tranValue.Value).ToList()
              };

最终会给我一个看起来像这样的结果:

myQuery = [{Type:"Alfa", Data: [50,60,10]},{Type:"Bravo", Data: [60,70,40,30]},{Type:"Charlie", Data: [10,30,5,15]}]

这是动态编写的方式但没有嵌套

var myQuery = trans
    .AsQueryable()
    .GroupBy("new(Type)", "new(Value)")
    .Select(@"new((Key.Type == ""A"" ? ""Alfa"" : (Key.Type == ""B"" ? ""Bravo"" : (Key.Type == ""C"" ? ""Charlie"" : (Key.Type == ""D"" ? ""Delta"" : ""Other"")))) as Type, """" AS Data)");

如何编写上面的动态查询以包含嵌套的值数组?

有人能指出一些好的例子,或者给我一些例子,包括如何分组和总结?

1 个答案:

答案 0 :(得分:0)

自己想出来。

问题是我正在做“new(Value)”,我应该只做“Value”。我看到一个使用 it 作为参数的示例查询。基本上表示当前项目。

这是Linq查询

var myQuery = from tran in trans
              group tran by tran.Type into tranGroup
              select new
              {
                  Type = (tranGroup.Key == "A" ? "Alfa" : (tranGroup.Key == "B" ? "Bravo" : (tranGroup.Key == "C" ? "Charlie" : (tranGroup.Key == "D" ? "Delta" : "")))),
                  Data = (from tranValue in tranGroup
                          select tranValue.Value).ToList()
              };

可以转录为动态查询

var myQuery = trans
    .AsQueryable()
    .GroupBy("new(Type)", "Value")
    .Select(@"new((Key.Type == ""A"" ? ""Alfa"" : (Key.Type == ""B"" ? ""Bravo"" : (Key.Type == ""C"" ? ""Charlie"" : (Key.Type == ""D"" ? ""Delta"" : ""Other"")))) as Type, it AS Data)");

会给我一个看起来像

的结果
myQuery = [{Type:"Alfa", Data: [50,60,10]},{Type:"Bravo", Data: [60,70,40,30]},{Type:"Charlie", Data: [10,30,5,15]}]
相关问题