简体   繁体   English

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

[英]System.Linq.Dynamic - How to convert from Linq to Dynamic syntax

I'm using the class called System.Linq.Dynamic ( http://msdn.microsoft.com/en-us/vstudio//bb894665.aspx ). 我正在使用名为System.Linq.Dynamic的类( http://msdn.microsoft.com/zh-cn/vstudio//bb894665.aspx )。 It is used to define dynamic Linq queries. 它用于定义动态Linq查询。 I need my queries to be dynamic so they can be defined at runtime. 我需要查询是动态的,以便可以在运行时定义它们。

I'll use an example to explain: There is a Transactions object containing a list of Transaction objects. 我将使用一个示例进行解释:有一个包含交易对象列表的交易对象。

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

Lets say I declare the Transactions object and fill it with some data: 可以说我声明了Transactions对象,并用一些数据填充了它:

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

I can then run a Linq query like so to extract some data from trans: 然后,我可以运行这样的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)
            };

That can be (not so easily) transcribed to the following dynamic query 可以(不太容易)转录为以下动态查询

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

What this does is does is give an object with Type as the grouped Type and Total as the Summed Value for the particular group. 这样做的目的是为一个对象提供“类型”作为分组的“类型”,并为“总和”作为特定组的“求和值”。 For example it may look like this: 例如,它可能看起来像这样:

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

I understand the basics of how dynamic queries work but there doesn't seem to any clear examples of any complex dynamic queries that includes an explanation of what is happening. 我了解动态查询如何工作的基础知识,但是似乎没有任何复杂的动态查询的清晰示例,包括对正在发生的事情的解释。

This is the part I cannot work out. 这是我无法解决的部分。 Given the following Linq query: 给定以下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()
              };

Would ultimately give me a result that looks like this: 最终会给我一个看起来像这样的结果:

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

This is how it would be written dynamically but without the nesting : 这是动态编写但没有嵌套的方式

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)");

How do I write the dynamic query above to include the nested array of values? 如何编写上面的动态查询以包含值的嵌套数组?

Can anyone point me in the direction of some good examples or give me some examples including how to group and sum? 谁能指出一些好的例子或给我一些例子,包括如何分组和求和?

Figured it out myself. 我自己想通了。

The problem was I was doing "new(Value)" above where I should have been doing just "Value" . 问题是我在上面应该只做“ Value”的地方做“ new(Value) I saw an example query that was using it as a parameter. 我看到了一个使用作为参数的示例查询。 Basically it means the current item. 基本上, 表示当前项目。

This is a Linq query 这是一个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()
              };

that can be transcribed into a dynamic query 可以转录为动态查询

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)");

that will give me a result that looks like 那会给我一个看起来像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM