Click here to Skip to main content
15,922,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Suppose that I have DropDownList that contain the name of the table's column's name. I used LINQ to SQL commands for querying. The code is like this:

C#
WSMDataClassesDataContext wsrdTable = new WSMDataClassesDataContext();
string SelectedParameter = DropDownListParameters.SelectedValue;
var DataFromWSRD = from wsm in wsrdTable.WeatherStationReportDetails
                   where wsm.DateTime >= Date1 && wsm.DateTime <= Date2
                   select wsm;
List<object> SelectedParam = new List<object>();
foreach (var temp in DataFromWSRD)
{
        SelectedParam.Add("here should be selected the column that I have its name");
}


or

C#
WSMDataClassesDataContext wsrdTable = new WSMDataClassesDataContext();
string SelectedParameter = DropDownListParameters.SelectedValue;
var DataFromWSRD = from wsm in wsrdTable.WeatherStationReportDetails
                   where wsm.DateTime >= Date1 && wsm.DateTime <= Date2
                   select new{ "here I want to choose the column that I have its name as string" };


I want to dynamically select the column from the table based on DropDownList

Thank you very much in advance.
Posted
Updated 28-Jul-11 0:57am
v4

1 solution

Well what do you want to select exactly??

C#
MyDatabaseEntities context = new MyDatabaseEntities();

string singleString = context.MyTable.FirstOrDefaut(/*Lambda Expression to select a specific row*/).MyColumnName;
List<string> stringList = context.MyTable.Select(/*Lambda Expression to select what column you want*/).ToList();
</string>


http://msdn.microsoft.com/en-us/library/bb397687.aspx[^] -- Lambda expressions explained.

I hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900