简体   繁体   English

如何使用LINQ查询类似SQL的CSV?

[英]How to query CSV like SQL using LINQ?

now i am first reading CSV file and load data into datatabel and then insert data from that data table to sql table. 现在,我首先读取CSV文件并将数据加载到datatabel ,然后将数据从该数据表插入到sql表中。 after dumping data into sql table then i fetch data with sql and use many clause with where for filtration purpose. 将数据转储到sql表中后,我将使用sql提取数据,并在其中使用许多子句进行过滤。

how can i use LINQ to read and query CSV file with many filtration clause and dump result into data table. 我如何使用LINQ读取和查询具有许多过滤子句的CSV文件并将结果转储到数据表中。

here i am putting sql which i used to grab data from db table after dumping csv data into db table. 在这里,我将用于将CSV数据转储到db表后从db表中获取数据的sql。 sql is generated in c# apps. sql是在c#应用程序中生成的。

        strSql = "select (select count(*) as incoming from " + tableName + " where direction='I' and ";
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) >='" + StartTime + "' and ";
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) <='" + EndTime + "' ";
        strSql = strSql + "and Is_Internal=0 and continuation=0 and RIGHT(convert(varchar,[call duration]),8)<> '00:00:00' ";
        strSql = strSql + "and party1name not in ('Voice Mail') and party1name not like 'VM %') as incoming, ";

        strSql = strSql + "(select count(*) as OutGoing from " + tableName + " ";
        strSql = strSql + "where direction='O' and ";
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) >='" + StartTime + "' and ";
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) <='" + EndTime + "' ";
        strSql = strSql + "and Is_Internal=0 and continuation=0  and party1name not in ('Voice Mail') ";
        strSql = strSql + "and party1name not like 'VM %') as OutGoing, ";

        strSql = strSql + "(select count(*) as CallTransfer from " + tableName + " ";
        strSql = strSql + "where continuation=1  and ";
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) >='" + StartTime + "' and ";
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) <='" + EndTime + "' ";
        strSql = strSql + "and RIGHT(convert(varchar,[call duration]),8)<> '00:00:00' and party1name not in ('Voice Mail') ";
        strSql = strSql + "and party1name not like 'VM %') as CallTransfer; ";

        strSql = strSql + "SELECT count(*) as UnansweredCalls_DuringBusinessHours from "
                        + tableName + " where direction='I' and " + Environment.NewLine;
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) >='" + StartTime + "' and ";
        strSql = strSql + "CONVERT(datetime,right([Call Start],8)) <='" + EndTime + "' ";
        strSql = strSql + "and RIGHT(convert(varchar,[call duration]),8)= '00:00:00' and [Ring duration]>0 " + Environment.NewLine;
        //strSql = strSql + "CONVERT(Varchar,CONVERT(datetime,[Call Start]),108) between '09:00:00' and '17:30:00' " + Environment.NewLine;
        strSql = strSql + "and party1name not in ('Voice Mail')  and party1name not like 'VM %' and party1name not like 'Line%'" + Environment.NewLine;

so please anyone see the sql and tell me how can i use the same clause when query csv file by LINQ. 因此,请任何人看到sql并告诉我在LINQ查询csv文件时如何使用相同的子句。

please show me what code i need to write to read csv file and as well query the data in csv file. 请告诉我我需要写什么代码才能读取csv文件以及查询csv文件中的数据。 please guide. 请指导。 thanks 谢谢

I would suggest using a library like https://www.nuget.org/packages/LINQtoCSV/ 我建议使用类似https://www.nuget.org/packages/LINQtoCSV/的库

it's quite featured and will simplify your life slightly. 它功能强大,可以稍微简化您的生活。

So you will need to declare your object, so 因此,您将需要声明您的对象,所以

public CallReportLine {

[CsvColumn(Name = "Call Start", FieldIndex = 1)]
public DateTime CallStart {get; set;}

}

then load it 然后加载

CsvContext cc = new CsvContext();
IEnumerable<CallReportLine> calls =    cc.Read<Product>("MyFirstCSV.csv");

then something like 然后像

calls.Where(call => call.CallStart >= StartTime && call.CallStart < EndTime)

I solve my issue this way and i like to share the code. 我用这种方式解决了我的问题,我喜欢分享代码。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TimeSpan StartTime = TimeSpan.Parse("09:00:00");
        TimeSpan EndTime = TimeSpan.Parse("09:30:00");

        List<PhoneData> oPhoneData = GetPhoneData(@"z:\smdr(backup12-04-2016).csv");
        dgList.DataSource = oPhoneData;

        int incomingCount = (from row in oPhoneData
                             where row.direction == "I"
                             && row.Call_Start.TimeOfDay >= StartTime
                             && row.Call_Start.TimeOfDay <= EndTime
                             && row.Is_Internal == 0
                             && row.continuation == 0
                             && row.call_duration.TotalSeconds > 0
                             && row.party1name != "Voice Mail"
                             && !row.party1name.StartsWith("VM ")
                             select 1).Count();

    }

    public List<PhoneData> GetPhoneData(string strFileName)
    {
        return File.ReadLines(strFileName)
            .Skip(1)
            .Where(s => s != "")
            .Select(s => s.Split(new[] { ',' }))
            .Select(a => new PhoneData
            {
                Call_Start = DateTime.Parse( a[0]),
                call_duration = TimeSpan.Parse(a[1]),
                Ring_duration = int.Parse(a[2]),
                direction = a[4],
                Is_Internal =Convert.ToInt32( a[8]),
                continuation = int.Parse( a[10]),
                party1name = a[13]
            })
            .ToList();
    }
}

public class PhoneData
{
    public DateTime Call_Start { get; set; }
    public TimeSpan call_duration { get; set; }
    public Int32 Ring_duration { get; set; }
    public string direction { get; set; }
    public Int32 Is_Internal { get; set; }
    public Int32 continuation { get; set; }
    public string party1name { get; set; }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM