Unable to produce a chart using linq in csharp

2019-01-29 12:11发布

问题:

I am coding in Microsoft Visual Studio 12 in ASP.net using c# and a noob at it.

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

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

User selects Car and should give me the a chart that has engine type in x-axis and total on y-axis.

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.

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();
    // ...??

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.

回答1:

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



回答2:

If your header row is in CSV file then

Car = Int32.Parse(parts[2])

is trying to parse "Car" to Int32.

Try reading file with:

File.ReadAllLines("testing.csv").Skip(1)

to skip header row.