当您有行时,是否使用LINQ从列名中获取列值?
我正在尝试将一些使用数据集的代码转换为LINQ。一些代码将列名作为字符串传递给其他函数 我是否可以轻松地将其改写为LINQ当您有行时,是否使用LINQ从列名中获取列值?,linq,dataset,Linq,Dataset,我正在尝试将一些使用数据集的代码转换为LINQ。一些代码将列名作为字符串传递给其他函数 我是否可以轻松地将其改写为LINQ string s = getElement(tr, elementName); private string getElement (tableRow re, string elementName){ if(tr[elementName] != null){ return tr[elementName].toString() } } 或:
string s = getElement(tr, elementName);
private string getElement (tableRow re, string elementName){
if(tr[elementName] != null){
return tr[elementName].toString()
}
}
或:
对GVS的答复:
转换为LINQ的原因是,在许多情况下,它更容易编码LINQ并获得更好的性能。这与stackoverflow的另一个问题有关:
我之所以需要将列名用作字符串,主要是因为列名作为输入字段的ID传递,然后使用AJAX(jquery)将它们发送回程序。为什么要使用LINQ将工作和清除代码转换成某些内容 编辑:LINQ是很好很酷的东西。但是,不要像许多XML爱好者(特别是第一个适配器)那样,将XML应用于所有内容,从而犯同样的错误
如果您需要将lambda与其他lambda组合在一起,请使用这种很酷的技术,答案是使用反射 获得价值
private string getElement (tableRow tr, string element){
string val = "";
try
{
val = tr.GetType().GetProperty(element).GetValue(tr, null).ToString();
}
catch //NULL value
{
val = "";
}
}
或者复制场景:
foreach (PropertyInfo c in tr.GetType().GetProperties())
{
thr.GetType().GetProperty(c.Name).SetValue(thr,
tr.GetType().GetProperty(c.Name).GetValue(tr, null), null);
}
我想是2.2。使用IQueryable将其用作where、select中的参数听起来很不错。你能给我举个例子吗?我对LINQ atm还很陌生,但我认为LINQ语法不会像您现在给出的代码示例那样清晰明了。
foreach (PropertyInfo c in tr.GetType().GetProperties())
{
thr.GetType().GetProperty(c.Name).SetValue(thr,
tr.GetType().GetProperty(c.Name).GetValue(tr, null), null);
}