2015-09-14 80 views
-2

我有一個數據輸入表單,用戶將在其中輸入DateFrom和DateTo字段。如何基於特定條件動態構建Linq查詢

Select From Date: <input type="text" id="datepickerfrom" name="datepickerfrom"/> 
Select To Date: <input type="text" id="datepickerto" name="datepickerto"/> 
<asp:Button ID="btnGetData" runat="server" OnClick="BtnGetData_Click" Text="Get Error List" /> 

我想構建一個Linq查詢,只檢索前100個記錄,如果沒有輸入提供。

如果用戶提供DateFrom並且沒有提供DateTo,用戶將選擇大於DateFrom的數據直到DateTime.Now。

如果用戶提供DateTo並且不提供DateFrom,則用戶將選擇小於DateTo的數據。

我現在有以下幾點:

public static List<ErrorLogData> GetLogErrorData(string appName, InputData data) 
{ 
    SqlConnection con; 
    List<ErrorLogData> errorLogData = null; 
    string query = ""; 
    if (data.DateFrom == "" && data.DateTo == "") 
    { 
     query += "from ld in logData.errorLogs.Take(10000)"; 
    } 

    if (data.DateFrom == "" && data.DateTo != "") 
    { 
     query += "from ld in logData.errorLogs where ld.errorTime <= " + data.DateTo; 
    } 

    if (data.DateFrom != "" && data.DateTo == "") 
    { 
     query += "from ld in logData.errorLogs where ld.errorTime >= " + data.DateFrom + " && <= " + DateTime.Now; 
    } 

    if (data.DateFrom != "" && data.DateTo != "") 
    { 
     query += "from ld in logData.errorLogs where ld.errorTime >= " + data.DateFrom + " && <= " + data.DateTo; 
    } 

    DateTime dateFrom = Convert.ToDateTime(data.DateFrom); 
    DateTime dateTo = Convert.ToDateTime(data.DateTo); 


    using (con = new SqlConnection(ConfigurationManager.AppSettings[conKey])) 
    using (WebEntities logData = new WebEntities()) 
    { 
     logData.CommandTimeout = 300; 
     var errorLog = query + 
         select new ErrorLogData 
         { 
          ErrorID = ld.errorID, 
          ErrorTime = ld.errorTime, 
          UserName = ld.username, 
          ErrorType = ld.errorType, 
          Error = ld.error, 
          ControlNumber = ld.controlNumber 
         }; 
     errorLogData = errorLog.ToList(); 
    } 
    return errorLogData; 
} 

我不知道如何追加query爲「select new ErrorLogData...」語句有整個查詢。

這裏有什麼方法?

+1

我想你錯過了SQL注入101 –

+2

是否使用實體框架的選項? –

+0

不一定。我可以使用常規方式。我爲了自己的緣故去學習Linq to Entity。試圖看看是否有辦法使用實體框架來實現我想要的功能 – gene

回答

1

您應該只能使用錯誤日誌的IQueryable結果,然後爲您的if語句執行lambda表達式。

  List<ErrorLogData> errorLogData = null; 
     DateTime dateFrom = Convert.ToDateTime(data.DateFrom); 
     DateTime dateTo = Convert.ToDateTime(data.DateTo); 


     //IQueryable errorLog 
     var errorLog = from ld in logData.errorLogs 
         select new ErrorLogData 
         { 
          ErrorID = ld.errorID, 
          ErrorTime = ld.errorTime, 
          UserName = ld.username, 
          ErrorType = ld.errorType, 
          Error = ld.error, 
          ControlNumber = ld.controlNumber 
         }; 



     if (data.DateFrom == "" && data.DateTo == "") 
     { 
      errorLogData = errorLog.Take(10000); 
     } 

     if (data.DateFrom == "" && data.DateTo != "") 
     { 
      errorLogData = errorLog.where(x => x.ErrorTime <= dateTo).ToList(); 
      //query += "from ld in logData.errorLogs where ld.errorTime <= " + data.DateTo; 
     } 

     //contine to implement If 


     return errorLogData; 
1

假設你正在使用某種形式的LINQ數據訪問技術,使用類似以下內容:

private List<Entity> GetData(DateTime? dateFrom, DateTime? dateTo) 
{ 
    IQueryable<Entity> query = ...; //Here reference your table 

    if (dateFrom == null && dateTo == null) 
    { 
     query = query.Take(100); 
    } 
    else 
    { 
     DateTime dateToValue = dateTo ?? DateTime.Now; 

     query = query.Where(x => x.Date <= dateToValue); 

     if (dateFrom != null) 
     { 
      query = query.Where(x => x.Date >= dateFrom.Value); 
     } 
    } 

    return query.ToList(); //This will actually execute the query. Here you can expand your query to select specific columns before executing ToList 
}