使用LINQ to SQL动态查询 [英] Dynamic query using LINQ to SQL

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

问题描述

我需要弄清楚它是否可以动态地构建LINQ,动态地选择在其中执行查询的表的查询。

I need to figure out if it is possible to dynamically build a query with LINQ, dynamically selecting the table in which to perform the query.

这是一个例子的我会做什么:

This is an example of what I would do:

//Not working,just for example

public List<dynamic> _getGenericList(String tableName)
    {
        var l = from a in db.//I need to use here tableName
                  select a;

        return l.ToList<dynamic>();
    }



有没有一种方法,使这一切成为可能?

Is there a way to make this possible?

推荐答案

如果查询是这样的简单,你可以动态地创建一个标准的SQL语句并执行它,这是不使用的处理器沉重的反思和复杂的代码最简单的方法?

If the query is this simple you can dynamically create a standard sql statement and execute it, this is the most simplest way without using processor heavy reflection and complex code?

var query = "SELECT * FROM " + tableName;
var res = context.ExecuteQuery<dynamic>(query).ToList();

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

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