[英]How to get value by column name entity framework linq?
I have a list of column names. 我有一个列名列表。 I want to iterate fetched data using list of column names, so what generic type should I use to convert fetched data into?
我想使用列名列表来迭代获取的数据,那么我应该使用哪种通用类型将获取的数据转换为?
Edit: 编辑:
List<string> columns = new List<string>();
columns.Add("column1");
columns.Add("column2");
columns.Add("column3");
string joinColumns = string.Join(",", columns);
string sql = "select "+joinColumns+" from Table";
It will fetched only selected columns, So how would I do same functionality using entity framework instead of using sql query? 它将仅获取选定的列,那么我将如何使用实体框架而不是使用sql查询来执行相同的功能?
Thanks in advance. 提前致谢。 Help is appreciated.
感谢帮助。
Solution- This is not possible in link so alternative to this, I converted List to DataTable using extension method described in the link - https://stackoverflow.com/a/5805044/1416033 解决方案-这在链接中是不可能的,因此对此替代,我使用链接中描述的扩展方法将List转换为DataTable- https://stackoverflow.com/a/5805044/1416033
List<string> columns = new List<string>();
columns.Add("column1");
columns.Add("column2");
columns.Add("column3");
List<Accounts> accountList = GetAccountList();
DataTable dt = accountList.ToDataTable<Accounts>();
foreach (DataRow dr in dt.Rows)
{
foreach (string column in columns)
{
val = dr[column].ToString();
}
}
What you describe isn't going to work with native LINQ functionality. 您所描述的内容不适用于本机LINQ功能。 There is a library though, that should help you.
虽然有一个图书馆,应该可以为您提供帮助。 Check out Dynamic LINQ: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
查看动态LINQ: http : //weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.