2016-04-22 8 views
1

私はまずCSVファイルを読み込み、datatabelにデータをロードしてから、そのデータテーブルからsqlテーブルにデータを挿入します。データをSQLテーブルにダンプした後、SQLでデータをフェッチし、多くの句をどこでフィルタリングの目的で使用します。LINQを使用してCSVをSQLのように照会する方法は?

どのようにLINQを使用して、多くのフィルタリング節とCSVファイルを読み込み、データテーブルに結果をダンプすることができますか。

ここで私はdbテーブルにcsvデータをダンプした後、dbテーブルからデータを取得するために使用したsqlを入れています。 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; 

はそう誰もが、SQLを見て、私はLINQで同じ句クエリcsvファイルを使用する方法を教えてください。

私はcsvファイルを読み込み、csvファイルのデータをクエリするために必要なコードを表示してください。ご案内ください。おかげ

答えて

0

のようなもの、その後

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

それをロードし、次にその

public CallReportLine { 

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

} 

、あなたのオブジェクトを宣言する必要があります。コードを共有する。

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

} 
5

は、私はそれはかなりおすすめだと少しあなたの人生を簡素化しますhttps://www.nuget.org/packages/LINQtoCSV/

のようなライブラリを使用してお勧めします。

だから、私はこの方法で私の問題を解決し、私は好き

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

リンクのみの回答は推奨されません(ただし、あなたは6年間メンバーになっています)。おそらく、その図書館の質問者の問題を解決することを目的とした例を投稿することができますか? –

+0

確かに - はい、私は正確なことについて謝罪することさえ知られています。 –

+0

どのように句を適用する私は私のSQLでlinqのcsvのために使用するときに使用? –

関連する問題