需要计数使用LINQ副本并将其绑定到图表 [英] Need to count duplicates using linq and bind it to chart

查看:104
本文介绍了需要计数使用LINQ副本并将其绑定到图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#编码在Microsoft Visual Studio在ASP.net。数据从.csv文件读取。

I am coding in Microsoft Visual Studio in ASP.net using C#. Data is read from a .csv file.

让说,我有3列由150行的表。
该设计如下:ID,发动机型号,租车。 ID列有一个通用的ID,发动机类型列由4个不同类型的发动机和车柱由1是,0为无。这意味着,是的,​​他们有车也没有,他们没有车。

Let say, I have a table that consists of 150 rows by 3 columns. The design is as follows: ID, Engine type, Car. The ID columns has a generic ID, engine type column consists of 4 different types of engines and Car column consists of a 1 for yes and 0 for no. This means that yes they have a car and no they don't have a car.

var myitems = from a in mylist  
              where (a.Car_ == "1") 
              select a.Engine_Type;

此只选择列,他们拥有一辆车。我用这个并将其绑定到我的表。

This only selects the columns where they "have" a car. I use this and bind it to my chart.

Chart1.DataSource = myitems.AsEnumerable().Distinct();
Chart1.Series[0].XValueMember = "Engine Type";
Chart1.Series[0].YValueMembers = "Car";
Chart1.DataBind()

我的查询绑定它来做图表完美,图表在x轴不同的发动机类型。
问题::这是问题,可以说,有137人是在车列,因此1。进出四种不同类型的发动机的其中135具有VTEC和它们的2具有拳击手。我想看看在这种情况下,适当的值列,VTEC = 135和拳击手= 2
因此,换句话说,我需要算多少VTEC和拳击手的在汽车列在那里,在同一图表上显示。目前,我用它在WHERE语句从表意13行不有车筛选出零行。

My query binds it do the chart perfectly, and the chart has different engine type in the x axis. PROBLEM:: This is the problem, lets say there are 137 people with yes so 1 in the car column. and out of the four different types of engines 135 of them have vtec and 2 of them have boxer. I would like to see columns of appropriate values in this case, vtec = 135 and boxer = 2 So in other words, i need to count how many vtec and boxer's are there in the car column and display it on the same chart. At the moment I use it in a where statement to filter out the zero rows from the table meaning 13 rows don't have a car.

推荐答案

您可以使用 GROUPBY子句。请参考下面code为相同:

You can use GroupBy clause. Please see below code for the same:

设计页面:

<asp:Chart runat="server" ID="chart1" Width="500px" Height="500px">
    <Series>
        <asp:Series ChartType="Line" Name="chartSeries" Color="Orange" LabelForeColor="Orange" Font="Tahoma, 11px, style=Bold"
            IsVisibleInLegend="True" BorderWidth="2" IsValueShownAsLabel="True" >
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="carArea" BackColor="Transparent">
            <AxisX Interval="1" TitleForeColor="White" >
                <LabelStyle ForeColor="White" Font="Tahoma, 12px, style=Bold" />
            </AxisX>
            <AxisY>
                <LabelStyle ForeColor="White" Font="Tahoma, 12px, style=Bold" />
            </AxisY>
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

code背后:

var chartData = mylist.Where(a => a.Car_ == "1").ToList();

foreach (var t in chartData.GroupBy(a => a.Engine_Type))
{
   chart1.Series[0].Points.AddXY(t.Key, t.Count());
}

这篇关于需要计数使用LINQ副本并将其绑定到图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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