Click here to Skip to main content
15,749,288 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Code project,

I am using c# , Linq to Entities with SQL server. I have a problem while writing a query using this.

Following is my present code.

C#
private List<DataAccount> GetDataAccountDetails()
{
PIAEntities piaentity = new PIAEntities(strConn);

List<DataAccount>dataAccou nt = (from p in piaentity.DataAccounts
               where p.FollowUpRequired == true && p.PreviousOwener == UserName        &&p.FollowUpOn < DateTime.Now
                              select p).ToList<DataAccount>();

}


After execution of this query it is returing entire database columns to DataGrid View. But my Client is expecting like he should have an option to select number of columns only. This means user will filter some columns some where from the datagrid view. I need to pass it to the above query and get those columns only.

With the above request it should like

C#
private List<DataAccount> GetDataAccountDetails(string[] columns)
{
PIAEntities piaentity = new PIAEntities(strConn);

List<DataAccount>dataAccou nt = (from p in piaentity.DataAccounts
               where p.FollowUpRequired == true && p.PreviousOwener == UserName        &&p.FollowUpOn < DateTime.Now
                              select p).ToList<DataAccount>();

}

the paramenter can be either string[] or only string. No Issues. Based on the passed paraments only i need to filter and get the results.

Hope explined clearly. Thanks in advance.

chowdary.
Posted

Try this:
C#
private List<dataaccount> GetDataAccountDetails(string[] columns)
{
    PIAEntities piaentity = new PIAEntities(strConn);
     
    var nt = from p in piaentity.DataAccounts
                                     where p.FollowUpRequired == true 
                                     && p.PreviousOwener == UserName   
                                     && p.FollowUpOn < DateTime.Now
                                     select new{
                                            Column1 = p.Column1,
                                            Column2 = p.Column2,
                                            Column3 = p.Column3,
                                            Column4 = p.Column4,
                                     };
    var customList = MakeList(nt);

}
public static List<T> MakeList<T>(T itemOftype)
{
    List<T> newList = new List<T>();
    return newList;
}   




--Amit
 
Share this answer
 
v4
Comments
Ganesh Nikam 14-Sep-12 7:06am    
when you select select new{
Column1 = p.Column1,
Column2 = p.Column2,
Column3 = p.Column3,
Column4 = p.Column4,
} it will be anonymous return type dude

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