0

hello how I do dynamic select column query ?

example

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



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

//I want to list only name column

var NameList = GetDynamicColumn("Name"); 

or

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.??? ;
}

and How do, I do dynamic select table query?

example

my sql table >> 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;
    }

thank you

  • possible duplicate of [LINQ : Dynamic select](http://stackoverflow.com/questions/16516971/linq-dynamic-select) (Just one of many). Please always first assume that your question has been asked before. Dynamic LINQ is a recurring topic at StackOverflow. – Gert Arnold Apr 26 '14 at 21:49

1 Answers1

0

For the second part of your question, If you are using Linq2SQL fro getting dynamic table data you can use this query

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

or for having filter (where) you can write:

public IQueryable<TEntity> GetDynamicTable<TEntity>(Expression<Func<TEntity, bool>> expression)
{
   return  _DbEntities.GetTable<TEntity>().Select(i => i).Where(expression).AsQueryable();
}
Reza
  • 18,865
  • 13
  • 88
  • 163
  • hi Thank you for the answer how I do dynamic select column query ? and I've installed Linq2SQL http://www.nuget.org/packages/FlexLabs.Glimpse.Linq2Sql but query did not work Error 2 'CrewNetixData.CrewEntities' does not contain a definition for 'GetTable' and no extension method 'GetTable' accepting a first argument of type 'CrewNetixData.CrewEntities' could be found (are you missing a using directive or an assembly reference?) C:\TFS\ShipProjects\ShipManager\Areas\CrewNetixData\Entity\Concrete\Crew.cs 505 32 CrewNetixData – user3575477 Apr 26 '14 at 08:47
  • @user3575477 DataContext has this method, if you are using Linq2SQL call getTable on DataContext, you can pass DataContext to youe method or have a general DataContext. http://msdn.microsoft.com/en-us/library/bb357220.aspx – Reza Apr 26 '14 at 08:50