简体   繁体   English

C#图表绑定到var

[英]C# Chart bind to var

I have var t from linq query with this structure: 我有来自linq查询的var t具有以下结构:

    [0] = { Type = "K", Count = 1 }
    [1] = { Type = "Z", Count = 8 }

and now I want to bind it to my chart with elegance (no foreach). 现在我想将其绑定到我的图表上(没有foreach)。 I try to do something like this: 我尝试做这样的事情:

    series1.Points.DataBindXY(t. ????, t.????);

but I don't know how to get "first column" and "second column" of my var. 但我不知道如何获得我的var的“第一列”和“第二列”。 Please help 请帮忙

Edit: My linq query: 编辑:我的linq查询:

                var t= (from oAction in Actions
                    group oAction by oAction.Type.Name into g
                    select new { Type = g.Key, Count = g.Count() }).ToArray();

I have never worked with ASP.Net charts, so I'm not sure this will work, but I think it should: 我从未使用过ASP.Net图表,因此我不确定这是否可行,但我认为应该这样做:

series1.Points.DataBindXY(t.Select(x => x.Type), t.Select(x => x.Count));

This will assign a collection of Type values as the x values and collection of Count values as y values. 这将把Type值的集合指定为x值,并将Count数值的集合指定为y值。

var t= (from oAction in Actions
                    group oAction by oAction.Type.Name into g
                    select new { Type = g.Key, Count = g.Count() }).ToArray();

All-right, so you are returning an "Anonymous" type. 好的,因此您将返回“匿名”类型。 You can definitely access t.Type or t.Count but the catch is you can do this only inside the method where this LINQ query is defined. 您绝对可以访问t.Type或t.Count,但是要注意的是,您只能在定义此LINQ查询的方法内执行此操作。 The scope of anonymous type is limited to the method in which it is defined. 匿名类型的范围限于定义它的方法。 You can overcome this limitation by using "dynamic" keyword. 您可以使用“动态”关键字来克服此限制。 But I haven't tried this, so can't be 100% sure. 但是我还没有尝试过,所以不能100%确定。

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

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