无法在 csharp 中使用 linq 生成图表 [英] Unable to produce a chart using linq in csharp

查看:10
本文介绍了无法在 csharp 中使用 linq 生成图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ASP.net 中的 Microsoft Visual Studio 12 中使用 c# 和一个菜鸟编码.

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 应该是一个通用的自动编号,Engine Type 应该是一个类型字符串,我不知道 Car 应该是什么,因为它由代表布尔值的 1 和 0 组成.这意味着 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() 将其转换为列表,然后使用以下代码将其绑定到下拉列表:

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

用户选择汽车,应该给我一个图表,在 x 轴上有引擎类型,在 y 轴上有总数.

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名vtec和2名拳击手.所以 vtecs 代表一列总共有 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 查询和以下图表绑定代码.

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 文件中读取我的列时.错误说Format exception was unhandled by user code: Input string was not in a正确格式

The error comes at the point where I am reading my columns from the csv files. The error says Format exception was unhandled by user code: Input string was not in a correct format

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; }
}

然后执行以下操作:

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

这篇关于无法在 csharp 中使用 linq 生成图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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