动态构建linq查询 [英] build linq queries dynamically

查看:74
本文介绍了动态构建linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串列表,例如

suppose I have a string list, like

list<string> cols = {"id", "name", "position"}.

此列表是动态生成的,此列表中的每个代表数据库表中的列名.

This list is generated dynamically, and each one in this list represents a column name in a database table.

我要做的是动态创建一个linq查询,该查询仅返回这些列.

what I want to do is create a linq query dynamically which returns these columns only.

var q = from e in employ
        select new {
          id = id,
          name = name,
          position = position
};

如何根据输入列列表生成类似的查询?

How can I generate a query like that based on the input column lists?

推荐答案

您可以使用表达式树来构建动态Linq查询. 这是一个示例: http://msdn.microsoft.com/zh-cn/library/bb882637.aspx

You can use Expression Trees to build dynamic Linq queries. Here is an example: http://msdn.microsoft.com/en-us/library/bb882637.aspx

这篇关于动态构建linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆