具有所有数据类型的C#LINQ动态选择 [英] C# LINQ Dynamic Select with all type of data
本文介绍了具有所有数据类型的C#LINQ动态选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用动态LINQ来实现搜索,其中查询在运行时获取列名和搜索值.这样,我需要根据列类型解析数据-
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();
}
可以使用单个where子句为所有数据类型完成此操作吗?
Can this be done for all data types using single where clause?
推荐答案
您可以使用表达式树来做到这一点.
You can do this using expression trees.
https://msdn.microsoft.com/en-us/library/bb397951.aspx
https://msdn.microsoft.com/en-us/library/bb882637.aspx
这篇关于具有所有数据类型的C#LINQ动态选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文