Chocoboboy가 말한 것처럼 System.Linq.Dynamic
이 도움이됩니다. 안타깝게도 .NET 프레임 워크에는 포함되어 있지 않지만 Scott Guthrie의 블로그에서 다운로드 할 수 있습니다. 귀하의 경우에는 Select(string selector)
(열 목록이 하드 코딩되었거나 목록에서 제공된 것)으로 전화해야합니다.
List<string> cols = new List<string>(new [] { "id", "name", "position" });
var employ = new[] { new { id = 1, name = "A", position = "Manager", salary = 100 },
new { id = 2, name = "B", position = "Dev", salary = 50 },
new { id = 3, name = "C", position = "Secretary", salary = 25 }
};
string colString = "new (id as id, name as name, position as position)";
//string colString = "new (" + (from i in cols select i + " as " + i).Aggregate((r, i) => r + ", " + i) + ")";
var q = employ.AsQueryable().Where("salary >= 50").Select(colString);
foreach (dynamic e in q)
Console.WriteLine(string.Format("{0}, {1}, {2}", e.id, e.name, e.position));
그러나이 방법은 어떻게 든 쿼리 강력한 형식의 LINQ의 목적은 패배, 그래서주의와 함께 사용할 것 : 선택적으로, 내 예는 동적 Where
절 (Where("salary >= 50")
)가 포함되어 있습니다.
동적 Linq http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query- library.aspx가이 작업을 수행 할 수 있습니다. – Chocoboboy