我有一個數據庫是這樣的:將數據傳送到時間範圍
DateAndTime Column1 Column2 Column3 ... Column85
============================================================
2016-01-01 00:00:10 1 0 0 0
2016-01-01 00:00:20 1 0 0 1
2016-01-01 00:00:30 1 1 0 1
2016-01-01 00:00:40 1 1 0 0
2016-01-01 00:00:50 0 1 0 0
2016-01-01 00:01:00 1 0 0 1
2016-01-01 00:01:10 1 0 0 1
2016-01-01 00:01:20 1 0 0 0
2016-01-01 00:01:30 0 0 0 0
...
2016-01-11 00:01:30 0 0 0 0
我使用LINQ檢索數據。
var Data = (from row in db._Data where row.Column86 == X select row).ToList();
是否有可能產生的結果輸出是這樣的或類似的:
Column1
From 2016-01-01 00:00:10 To 2016-01-01 00:00:50
From 2016-01-01 00:01:00 To 2016-01-01 00:01:30
Column2
From 2016-01-01 00:00:30 To 2016-01-01 00:01:00
...
Column85
From 2016-01-01 00:00:20 To 2016-01-01 00:00:40
From 2016-01-01 00:01:00 To 2016-01-01 00:01:20
以下是我嘗試代碼:
bool alarmhistory = false; // Check if there is a true for whole table
bool from = false; // If previous 1 is activate
bool gotAlarm = false; // Check if there is a true for whole column
string RawHtml = "";
string FinalHtml = "";
RawHtml = "<tr><td style='text - align:center; '><b>";
RawHtml += "Column1";
RawHtml += "</b></td></tr>";
RawHtml += "<tr><td>";
for (int x = 0; x < Data.Count ; x++)
{
if (x == 0 && Data.Count != 1) //First Row
{
if (Data[x].Column1 == 1)
{
gotAlarm = true;
alarmhistory = true;
from = true; // Start the time
RawHtml += "From " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
}
}
else if (x == 0 && Data.Count == 1) //First Row but only have 1 record
{
if (Data[x].Column1 == 1)
{
gotAlarm = true;
alarmhistory = true;
from = false; // Only 1 record, meaningless
RawHtml += "From " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
RawHtml += " To " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
}
}
else if (x == (Data.Count - 1)) //Last Row
{
if (Data[x].Column1 == 1)
{
gotAlarm = true;
alarmhistory = true;
if (from == true)
{
RawHtml += " To " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
}
if (from == false)
{
RawHtml += "From " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
RawHtml += " To " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
}
}
if (Data[x].Column1 == 0)
{
if (from == true)
{
RawHtml += " To " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
}
}
}
else //Others condition
{
if (Data[x].Column1 == 1)
{
gotAlarm = true;
alarmhistory = true;
if (from == false) // If previous is 0
{
RawHtml += "From " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
from = true; // Start the time
}
}
if (Data[x].Column1 == 0)
{
if (from == true) // If previous is 1
{
RawHtml += " To " + Data[x].DateAndTime.ToString("yyyy-MMM-dd hh:mm:ss tt");
RawHtml += "<br />";
from = false; //End the time
}
}
}
}
RawHtml += "</td></tr>";
if (gotAlarm == true)
{
FinalHtml += RawHtml;
}
RawHtml = "";
gotAlarm = false;
from = false;
之後,我將重複和更改Column1到Column2。但是我認爲Column16之後系統會堆棧溢出。
什麼格式的輸出應該是在一個字符串?自定義數據結構?答案必須完全在LINQ中嗎?你有嘗試過什麼嗎?如果是這樣,發佈你已經嘗試過。 – Quantic
是的,一個字符串。實際上是一種Html格式。不限於LINQ。邑我試着做85個循環請求列,但是當我達到16列系統堆棧溢出。 – DragonZelda