简体   繁体   中英

C# LINQ Dynamic Select with all type of data

I am implementing search by using dynamic LINQ, where the query gets column name and search value in runtime. In this way, I need to parse the data according to the column type-

if (isNumeric)
{
  int x = Int32.Parse(txtHistorySearch.Text);
  truncatedData = ((IQueryable<object>)rawData).Where(columnName + "=@0", x).ToList();
}
else if (DateTime.TryParse(txtHistorySearch.Text, out temp))
{
  var parsedDt = DateTime.Parse(txtHistorySearch.Text);
  var nextDay = parsedDt.AddDays(1);
  truncatedData = ((IQueryable<object>)rawData).Where(columnName + ">= @0 && " + columnName + " < @1", parsedDt, nextDay).ToList();
}
else
{
 truncatedData = ((IQueryable<object>)rawData).Where(columnName + "=@0", searchValue).ToList();
}

Can this be done for all data types using single where clause?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM