Linq动态选择列和表

时间:2014-04-26 08:18:26

标签: linq dynamic

你好我怎么做动态选择列查询?

例如

 CrewEntities _DbEntities = new CrewEntities(); // I'm using object context



  public class CrewItem
        {
            public string Name { get; set; }
            public string LastName { get; set; }
        }

//我想只列出名称列

var NameList = GetDynamicColumn("Name"); 

I want to list only LastName column 

var LastNameList = GetDynamicColumn("Name");



publict List<CrewItem> GetDynamicColumn(string Columm)
{
   var query = from p in _DbEntities.t_Crew
               select p.??? ;
}

和怎么做,我做动态选择表查询?

例如

我的sql表&gt;&gt; t_Crew,t_Person

var crewTable = GetDynamicTable("t_Crew");
var personTable = GetDynamicTable("t_Person");


    publict List<CrewItem> GetDynamicTable(string tableName)
    {
       var query = from p in _DbEntities.???
                   select p;
    }

谢谢

1 个答案:

答案 0 :(得分:0)

对于问题的第二部分,如果您使用Linq2SQL获取动态表数据,则可以使用此查询

public IQueryable<TEntity> GetDynamicTable<TEntity>()
{
   return  _DbEntities.GetTable<TEntity>().Select(i => i);
}

或者有过滤器(在哪里)你可以写:

public IQueryable<TEntity> GetDynamicTable<TEntity>(Expression<Func<TEntity, bool>> expression)
{
   return  _DbEntities.GetTable<TEntity>().Select(i => i).Where(expression).AsQueryable();
}