如何计算csv中的相似值并导入asp.net(C#)gridview?

如何计算csv中的相似值并导入asp.net(C#)gridview?,c#,asp.net,csv,C#,Asp.net,Csv,我有这样的CSV(使用管道分隔符) 我要做的是在gridview中显示条目的数量,比如 45-3 55-1 65-1 我怎样才能做到这一点 我现在正在做这件事 // get all lines of csv file string[] str = File.ReadAllLines(Server.MapPath("Test.csv")); // create new datatable DataTable dt = new DataTable(); //

我有这样的CSV(使用管道分隔符)

我要做的是在gridview中显示条目的数量,比如

45-3
55-1
65-1
我怎样才能做到这一点

我现在正在做这件事

//  get all lines of csv file
    string[] str = File.ReadAllLines(Server.MapPath("Test.csv"));

    // create new datatable
    DataTable dt = new DataTable();

    // get the column header means first line
    string[] temp = str[0].Split('|');

    // creates columns of gridview as per the header name
    foreach (string t in temp)
    {
        dt.Columns.Add(t, typeof(string));
    }

    // now retrive the record from second line and add it to datatable
    for (int i = 1; i < str.Length; i++)
    {
        string[] t = str[i].Split('|');
        dt.Rows.Add(t);

    }

    // assign gridview datasource property by datatable
    GridView1.DataSource = dt;

    // bind the gridview
    GridView1.DataBind();
//获取csv文件的所有行
字符串[]str=File.ReadAllLines(Server.MapPath(“Test.csv”);
//创建新数据表
DataTable dt=新的DataTable();
//获取列标题表示第一行
字符串[]temp=str[0]。拆分(“|”);
//根据标题名称创建gridview的列
foreach(temp中的字符串t)
{
添加(t,typeof(string));
}
//现在从第二行检索记录并将其添加到datatable
对于(int i=1;i
它立即打印出csv中的所有数据

//  get all lines of csv file
    string[] str = File.ReadAllLines(Server.MapPath("Test.csv"));

    // create new datatable
    DataTable dt = new DataTable();

    // get the column header means first line
    string[] temp = str[0].Split('|');

    // creates columns of gridview as per the header name
    foreach (string t in temp)
    {
        dt.Columns.Add(t, typeof(string));
    }

    // now retrive the record from second line and add it to datatable
    for (int i = 1; i < str.Length; i++)
    {
        string[] t = str[i].Split('|');
        dt.Rows.Add(t);

    }

    // assign gridview datasource property by datatable
    GridView1.DataSource = dt;

    // bind the gridview
    GridView1.DataBind();
var data = File.ReadAllLines(Server.MapPath("Test.csv"))
               .Select(s => s.Split('|')[1].Trim())
               .GroupBy(s => s)
               .Select(s => new 
                {
                    Value = s.Key,
                    Count = s.Count()
                })
               .ToList();
GridView1.DataSource = data;
GridView1.DataBind();
将为您提供:

Value   Count
 45       3
 55       1
 65       1

你确定那是CSV文件吗?你能展示一些实际的代码吗?谢谢,这很有用
GridView1.DataSource = File.ReadAllLines(Server.MapPath("Test.csv")).GroupBy(line => new { l = line.Split('|')[1] }).Select(a => new { text = a.Key.l + "-" + a.Count() }).ToArray();