无法出示使用LINQ CSHARP图表 [英] Unable to produce a chart using linq in csharp
问题描述
我使用C#和它一noob编码在ASP.net微软的Visual Studio 12。
I am coding in Microsoft Visual Studio 12 in ASP.net using c# and a noob at it.
这是什么CSV文件看起来像
This is what csv file looks like
ID, Engine Type, Car
111,vtec, 1
131,vtec, 1
157,boxer,1
148,boxer,1
167,vtec,1
158,,0
107,,0
ID应该是一个通用的自动编号,发动机型号应该是一个字符串类型,我不知道是什么车应该是因为它包含了1和0的重新presenting布尔。这意味着,1客户拥有一辆汽车和0表示客户没有一辆车
ID should be a generic autonumber, Engine Type should be a type string and I don't know what Car should because it consists off 1's and 0's representing Boolean. This means that 1-customer has a car and 0 means that customer doesn't have a car.
这是我怎么搞的它创建一个列表。
This is how i create a list out of it.
var testingobject = (
from line in File.ReadAllLines("testing.csv").Skip(0)
let parts = line.Split(',')
select new
{
ID = parts[0],
Engine_Type = parts[1],
Car = parts[2] //should i put int32.parse(parts[2]) here
).ToList();
我创建了一个简单的数组其中包括ID,发动机类型,租车它使用了ToList()转换到一个列表
,然后将其绑定到一个DropDownList,使用这种code:
I create a simple array which consists of ID,Engine Type,Car convert it to a list using ToList()
and then bind it to a dropdownlist, using this code:
string[] testingddl = new string[] { "ID", "Engine Type", "Car" };
List<String> mytestinglist = testingddl.ToList();
var mytestin = from m in mytestinglist
select m;
DropDownList1.DataSource = mytestin.AsEnumerable();
DropDownList1.DataTextField = "";
DropDownList1.DataValueField = "";
DropDownList1.DataBind();
用户选择汽车和应该给我一个具有上y轴在X轴和总引擎类型的图表。
User selects Car and should give me the a chart that has engine type in x-axis and total on y-axis.
问题:该列由1和0这意味着客户是否有车(1)或犯规(0)的结果。
我想查看有多少用户不同类型的发动机类型,并将其绑定到一个,说一个柱形图。因此数据应该显示在x轴,发动机型号和y轴应具有总。根据数据,有3 vtecs和2的拳击手。所以vtecs重新present 3和总一个拳击手柱2。
The Problem: The column consists of 1's and 0's meaning whether the customer has a car (1) or doesnt (0).
I would like to view how many users have different types of engines types and bind it to a, say a column chart. So the data should display on the x-axis, engine type and y-axis should have the total. according to the data, there are 3 vtecs and 2 boxers. So vtecs represent a column with 3 total and boxers with 2.
我用这个LINQ查询和下面的图表结合code。
I use this linq query and the following chart binding code.
if (tempval == "Car")// this is the current selected dropdown list value
{
var myfavitems = testingobject.Where(a => a.Car == "1").ToList();
foreach (var t in myfavitems.GroupBy(a => a.Engine_Type))
{
Series Series1 = new Series();
Chart1.Series.Add(Series1);
Chart1.Series[1].Points.AddXY(t.Key.ToString(), t.Count().ToString()).ToString();
Chart1.DataSource = myfavitems.AsEnumerable();
Chart1.Series[1].XValueMember = "Group Equipment Type";
Chart1.Series[1].YValueMembers = "Software";
Chart1.DataBind();
// ...??
错误正值我在哪里从CSV文件中读取我的专栏的地步。错误说格式的例外是由用户code未处理:输入字符串的不正确的格式
select new
{
ID = parts[0],
Engine_Type = parts[1],
Car = Int32.Parse(parts[2])
}
我不知道什么是错的这种说法。可能有人请帮助我。
I dont know what is wrong with that statement. Could someone please help me out.
推荐答案
您会希望有一个像这样的类:
You will want to have a class like this:
public class CarClass {
public int Id { get; set; }
public string Engine { get; set; }
public int Car { get; set; }
}
然后做这样的事情:
And then do something like:
var car_collection =
from line in File.ReadAllLines("your_csv_name.csv").Skip(1)
let parts = line.Split(',')
select new CarClass()
{
Id = Int32.Parse(parts[0]),
Engine = parts[1],
Car = Int32.Parse(parts[2])
};
var listy = car_collection.ToList();
另外,请注意{}和(),因为你的不匹配:
Otherwise, notice the '{ }' and the '( )' , because yours don't match:
var car_collection =
(from line in File.ReadAllLines("your_csv_name.csv").Skip(1)
let parts = line.Split(',')
select new CarClass()
{
Id = Int32.Parse(parts[0]),
Engine = parts[1],
Car = Int32.Parse(parts[2])
}).ToList();
这应该与错误解决您的问题。
That should solve your problem with the error
这篇关于无法出示使用LINQ CSHARP图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!