如何使用LINQ查询像SQL一样的CSV?

Mon*_*kar 1 c# linq csv

现在我首先阅读CSV文件并将数据加载到datatabel该数据表中然后将数据插入到sql表中.在将数据转储到sql表之后,我使用sql获取数据并使用许多子句来处理过滤目的.

如何使用LINQ读取和查询具有多个过滤子句的CSV文件并将结果转储到数据表中.

在这里我把sql用于在将csv数据转储到db表之后从db表中获取数据.sql是在c#apps中生成的.

        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;
Run Code Online (Sandbox Code Playgroud)

所以请任何人看到SQL并告诉我如何在LINQ查询csv文件时使用相同的子句.

请告诉我我需要写什么代码来读取csv文件,以及查询csv文件中的数据.请指导.谢谢

Mik*_*ler 6

我建议使用像https://www.nuget.org/packages/LINQtoCSV/这样的库

它非常有特色,可以略微简化您的生活.

所以你需要声明你的对象,所以

public CallReportLine {

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

}
Run Code Online (Sandbox Code Playgroud)

然后加载它

CsvContext cc = new CsvContext();
IEnumerable<CallReportLine> calls =    cc.Read<Product>("MyFirstCSV.csv");
Run Code Online (Sandbox Code Playgroud)

然后像

calls.Where(call => call.CallStart >= StartTime && call.CallStart < EndTime)
Run Code Online (Sandbox Code Playgroud)

  • 不鼓励仅限链接的答案(但您知道,您已经成为会员已有六年了).也许你可以发布一个旨在解决提问者对该库问题的例子? (2认同)