0

I'm using the class called System.Linq.Dynamic (http://msdn.microsoft.com/en-us/vstudio//bb894665.aspx). It is used to define dynamic Linq queries. 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 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:

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:

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?

4

1 回答 1

0

我自己想通了。

问题是我在上面做"new(Value)",而我应该只做"Value"。我看到了一个使用作为参数的示例查询。基本上意味着当前项目。

这是一个 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]}]
于 2013-03-06T00:16:24.503 回答