2014-05-09 17 views
0

我正在嘗試寫一些將允許自定義表達式存儲在字符串中以評估各種數據的東西。在幾次失敗的嘗試之後,我找到了一些成功的東西,但它不適合這種意圖。使用LINQ來根據數據表從字符串計算表達式

DataTable dt = new DataTable(); 
dt.Columns.Add(new DataColumn("Name", typeof(string))); 
dt.Columns.Add(new DataColumn("Age", typeof(int))); 

dt.Rows.Add(new object[] { "John", 32 }); 

var test = dt.AsEnumerable().AsQueryable(); 

// An example of an expression to test 
string expression = "Age = 32"; 

// Failed Attempts 
//var result1 = test.Where(expression); // Error received: {"No property or field 'Age' exists in type 'DataRow'"} 
//var result2 = test.Where("it.Field<object>(\"Age\") == 32"); // Error received: {"No property or field 'Field' exists in type 'DataRow'"} 
//var result3 = test.Where("[email protected]", 32); // Error received: {"No property or field 'Age' exists in type 'DataRow'"} 
//var result4 = test.Where("it[\"Age\"] == 127"); // Error received: {"Operator '==' incompatible with operand types 'Object' and 'Int32'"} 
//var result5 = test.Where("it[\"Age\"] = 127"); // Error received: {"Operator '=' incompatible with operand types 'Object' and 'Int32'"} 
//var result6 = test.Where("it[\"Age\"] as Age = 127"); // Error received: {"Expression of type 'Boolean' expected"} 

// Successful but at this point its not really a dynamic linq expression 
var result = test.Where(it => it.Field<int>("Age") == 32); 

我該如何做到這一點?所有的道路似乎都回到這篇文章:http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx但是我沒有看到我正在使用強類型數據時所要做的事情的答案。

謝謝!

+3

這不是'linq',而是[ 'DataTable.Select()'](http://msdn.microsoft.com/en-us/library/system.data.datatable.select.aspx)方法會有幫助嗎? – DaveParsons

+0

我從來沒有嘗試過使用Dynamic Linq和DataTable。我想知道你是否會考慮不使用DataTable的選項? –

+0

這將工作:dt.Select(「年齡= 32」);這也會起作用:dt.AsEnumerable()。where(a => a [「Name」]。Equals(「John」)); – Arie

回答

2

如果你想使用DynamicLINQ你需要改變你的代碼沒有列...。

1)此表達

// Failed Attempts 
string expression = "Age = 32"; 
var result1 = test.Where(expression); // Error received: {"No property or field 'Age' exists in type 'DataRow'"} 

是相同

// Failed Attempts 
var result1 = test.Where("Age = 32"); // Error received: {"No property or field 'Age' exists in type 'DataRow'"} 

所以DynamicLinq嘗試找物業Age的DataRow和自的DataRow類沒有這個屬性,你得到錯誤'DataRow'類型中不存在屬性或字段'年齡'


2)在此表達

var result2 = test.Where("it.Field<object>(\"Age\") == 32"); // Error received: {"No property or field 'Field' exists in type 'DataRow'"} 

DynamicLinq試圖找到屬性或方法Field,但它的擴展方法DataRowExtensions,所以你也可以得到錯誤無屬性或字段「現場」的類型存在'的DataRow'


3)這個表達式是相同的第一使用參數

var result3 = test.Where("[email protected]", 32); // Error received: {"No property or field 'Age' exists in type 'DataRow'"} 

下一頁兩種表達等價的,監守當DynamicLinq解析表達式,它解析既===Equals

var result4 = test.Where("it[\"Age\"] == 127"); // Error received: {"Operator '==' incompatible with operand types 'Object' and 'Int32'"} 
var result5 = test.Where("it[\"Age\"] = 127"); // Error received: {"Operator '=' incompatible with operand types 'Object' and 'Int32'"} 

,如果你看到DataRow.Item屬性,你可以看到它返回Object,所以檢查等於你需要投的對象結果到int像這樣的東西

var result4 = test.Where("Convert.ToInt32(it[\"Age\"]) == 127"); 

也可能,在這種情況下,你可以幫助paramtrize形式這樣

var result4 = test.Where("it[\"Age\"] == @0",127); 

終於在這裏

var result6 = test.Where("it[\"Age\"] as Age = 127"); // Error received: {"Expression of type 'Boolean' expected"} 

解析表達式DynamicLinq得到拉姆達不返回Boolean後,所以你得到錯誤'布爾'類型的表達預期

-1

這應該讓你找到你想要的。把你的空檢查的情況下有匹配提供的字符串名稱等

int age = 32; 
string column = "Age"; 

DataTable dt = new DataTable(); 
dt.Columns.Add(new DataColumn("Name", typeof(string))); 
dt.Columns.Add(new DataColumn("Age", typeof(int))); 

dt.Rows.Add(new object[] { "John", 32 }); 

var result = dt.AsEnumerable().Where(r => Convert.ToInt32(r[column]) == age); 
+0

那個人正在尋找動態的linq ... –

相關問題