I'm trying to convert some code that uses datasets to LINQ. Some of the code passes column names into other functions as strings.
我正在尝试将一些使用数据集的代码转换为LINQ。一些代码将列名称作为字符串传递给其他函数。
Is there anyway I can easily rewrite this into LINQ?
无论如何,我可以轻松地将其重写为LINQ?
string s = getElement(tr, elementName);
private string getElement (tableRow re, string elementName){
if(tr[elementName] != null){
return tr[elementName].toString()
}
}
OR:
private void copy (tableRow trFrom, tableRow trTo){
foreach (DataColumn c in trFrom.Table.Columns) {
trTo[c.ColumnName] = trFrom[c.ColumnName];
}
}
Answer to GVS: The reason to convert to LINQ is because it in many situations are easier to code LINQ and get better performance. It's related to another question here on *: programming pattern using typed datasets
回答GVS:转换为LINQ的原因是因为它在很多情况下更容易编写LINQ代码并获得更好的性能。它与*中的另一个问题有关:使用类型化数据集的编程模式
The reason I need to use the column name as a string is mainly because the column names are passed as a ID for input fields, they are then sent back to the program using AJAX (jquery).
我需要将列名用作字符串的原因主要是因为列名作为输入字段的ID传递,然后使用AJAX(jquery)将它们发送回程序。
3 个解决方案
#1
2
-
Simple way
1.1. Using IEnumerable (Linq to Objects or similar) Change the elementName parameter for a Func and pass lambdas instead (you will get compile time checking as well!)
1.1。使用IEnumerable(Linq to Objects或类似)更改Func的elementName参数并改为传递lambdas(您也将获得编译时检查!)
1.2. Using IQueryable (Linq to SQL or similar) Same, but use Expression> instead.
1.2。使用IQueryable(Linq to SQL或类似)相同,但使用Expression>。
-
Complex way: If for some reason you need to keep the parameter as an string (maybe is introduced by user) You can use reflection to build an expression tree at runtime http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.aspx.
复杂的方式:如果由于某种原因你需要将参数保存为字符串(可能由用户介绍)你可以使用反射在运行时构建表达式树http://msdn.microsoft.com/en-us/library/ system.linq.expressions.expression.aspx。
2.1. Using IEnumerable Then Compile, and use it as a parameter in a where, select...
2.1。使用IEnumerable然后编译,并将其用作where中的参数,选择...
2.2. Using IQueryable Use it as a parameter in the where, select,
2.2。使用IQueryable将其用作where,select中的参数,
If you need to compose the lambda with other lambdas use this cool technique http://tomasp.net/blog/linq-expand.aspx
如果你需要用其他lambda编写lambda,请使用这个很酷的技术http://tomasp.net/blog/linq-expand.aspx
#2
1
The answer is to use reflection.
答案是使用反射。
Getting a value
获得价值
private string getElement (tableRow tr, string element){
string val = "";
try
{
val = tr.GetType().GetProperty(element).GetValue(tr, null).ToString();
}
catch //NULL value
{
val = "";
}
}
Or the copy scenario:
或复制方案:
foreach (PropertyInfo c in tr.GetType().GetProperties())
{
thr.GetType().GetProperty(c.Name).SetValue(thr,
tr.GetType().GetProperty(c.Name).GetValue(tr, null), null);
}
#3
-1
Why would you convert working and clear code into something using LINQ?
为什么要使用LINQ将工作和清除代码转换为某些内容?
Edit: LINQ is nice, cool stuff. But don't make the same errors as many XML fans did (specially first-adapters), by applying it to everything.
编辑:LINQ很好,很酷。但是,不要像许多XML粉丝那样做出同样的错误(特别是第一个适配器),将其应用于所有内容。
#1
2
-
Simple way
1.1. Using IEnumerable (Linq to Objects or similar) Change the elementName parameter for a Func and pass lambdas instead (you will get compile time checking as well!)
1.1。使用IEnumerable(Linq to Objects或类似)更改Func的elementName参数并改为传递lambdas(您也将获得编译时检查!)
1.2. Using IQueryable (Linq to SQL or similar) Same, but use Expression> instead.
1.2。使用IQueryable(Linq to SQL或类似)相同,但使用Expression>。
-
Complex way: If for some reason you need to keep the parameter as an string (maybe is introduced by user) You can use reflection to build an expression tree at runtime http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.aspx.
复杂的方式:如果由于某种原因你需要将参数保存为字符串(可能由用户介绍)你可以使用反射在运行时构建表达式树http://msdn.microsoft.com/en-us/library/ system.linq.expressions.expression.aspx。
2.1. Using IEnumerable Then Compile, and use it as a parameter in a where, select...
2.1。使用IEnumerable然后编译,并将其用作where中的参数,选择...
2.2. Using IQueryable Use it as a parameter in the where, select,
2.2。使用IQueryable将其用作where,select中的参数,
If you need to compose the lambda with other lambdas use this cool technique http://tomasp.net/blog/linq-expand.aspx
如果你需要用其他lambda编写lambda,请使用这个很酷的技术http://tomasp.net/blog/linq-expand.aspx
#2
1
The answer is to use reflection.
答案是使用反射。
Getting a value
获得价值
private string getElement (tableRow tr, string element){
string val = "";
try
{
val = tr.GetType().GetProperty(element).GetValue(tr, null).ToString();
}
catch //NULL value
{
val = "";
}
}
Or the copy scenario:
或复制方案:
foreach (PropertyInfo c in tr.GetType().GetProperties())
{
thr.GetType().GetProperty(c.Name).SetValue(thr,
tr.GetType().GetProperty(c.Name).GetValue(tr, null), null);
}
#3
-1
Why would you convert working and clear code into something using LINQ?
为什么要使用LINQ将工作和清除代码转换为某些内容?
Edit: LINQ is nice, cool stuff. But don't make the same errors as many XML fans did (specially first-adapters), by applying it to everything.
编辑:LINQ很好,很酷。但是,不要像许多XML粉丝那样做出同样的错误(特别是第一个适配器),将其应用于所有内容。