2011-09-02 47 views
3

有一個呼叫列表,包括starttime,stoptime,duration,caller和其他一些屬性。現在我試着從這個列表中看到併發呼叫,但是我不知道如何開始這個任務。我想在.net C#中實現這個任務。計數來自Calllog的併發呼叫

的第一個想法就是把每個呼叫,它算到一個Array,每一秒,然後我有一個數組至極計數呼籲每一秒,但它不是那麼好,我認爲:-)

class CallConcurrentCounterController 
{ 
    static void Main(string[] args) 
    {  
     var Calls = new ImportDataFromCSV(); 
     DataTable dataTable = Calls.GetDataTable(Path,Seperator); 

     DateTime startTime = new DateTime(2011,07,01);    
     callsBucket(dataTable,startTime);    
    } 
    public void callsBucket(DataTable Calls, DateTime startTime) 
    { 
     var callsBuckets = 
      from time in Enumerable.Range(0, (60 * 24)) 
       .Select(i => startTime.AddMinutes(i)) // Create the times base on start time and current minute.     
      select new // Create an anonymous type 
      { 
       // Create a property called Time, that stores the time checked 
       Time = time, 
       // Another property that stores the number of calls happening at that time. 
       //CallCount = Calls.Rows.Count(Call => Call.start <= time && Call.stop >= time)      
       CallCount = Calls.AsEnumerable().Count 
        (Call => DateTime.Parse(Call.ItemArray[0].ToString() + " " + Call.ItemArray[1].ToString()) <= time && 
          DateTime.Parse(Call.ItemArray[0].ToString() + " " + Call.ItemArray[2].ToString()) >= time) 
      }; 
    } 
} 

從調用此函數的函數獲得帶有n行的DataTable每行都是一個帶有屬性的調用start ItemArray [1](用於啓動時間),停止ItemArray [2](用於停止時間),日期ItemArray [0]和其他一些屬性,如呼叫者號碼...

現在我得到一個桶分鐘計數的電話。但我怎麼能將CallBuckets的類型轉換爲類似Enumerable的類型?

回答

2

像下面的東西應該讓你開始。 這從給定的開始時間創建分鐘窗口(覆蓋全天,每小時60分鐘,每天24小時)。

然後計算所有之前開始的呼叫,並在此之後結束。您可以將Count呼叫更改爲Where,以保留每次發生的單個呼叫的詳細信息。

var startTime = new DateTime.Today().AddDays(-1); //When to start the minute interval buckets 
var callsBuckets = 
    from time in Enumerable.Range(0, (60 * 24) // Minutes in a day 
          .Select(i => new DateTime(startTime).AddMinutes(i) // Create the times base on start time and current minute. 
    select new // Create an anonymous type 
    { 
     // Create a property called Time, that stores the time checked 
     Time = time, 
     // Another property that stores the number of calls happening at that time. 
     CallCount = Calls.Count(Call => Call.StartTime <= time && Call.StopTime >= time) 
    }; 

現在你已經顯示您使用的是DataTable,看到接受這個問題的答案:LINQ query on a DataTable

+0

對變量使用大寫字母令人困惑。 – Amy

+0

更正的外殼 –

+0

嗨喬治,我測試你的代碼,但得到一些錯誤,我用Calls讀取CSV並將其寫入到一個名爲「Calls」的DataTable中,然後使用參數初始化函數:調用startTime stopTime並在裏面寫入Code ,但我得到一個錯誤,沒有接受DataRowCollection的函數Count。 – kockiren

1

您需要先編寫一個方法來確定兩個DateTime範圍是否重疊。一旦該方法正常工作,只需在列表中查找具有最多重疊次數的呼叫。或者,如果您需要確定在特定時間的電話號碼,只需編寫一個方法來檢查範圍是否覆蓋此時間值,然後在您的電話列表中執行Count()

+0

你能解釋我如何檢查兩個DateTime Vars的範圍重疊嗎? – kockiren

+0

就像這樣:'私人布爾相交(int r1start,詮釋r1end,詮釋r2start,詮釋r2end) { return(r1start == r2start)|| (r1start> r2start?r1start <= r2end:r2start <= r1end); }' –