Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
by passing single column name binding records but i want to get records by passing multiple column names dynamically using entity framework

What I have tried:

public List<string> GetRecords(List<string> Columns,string tablename)
    {
        using (var Context = new MCPEntities())
        {
             string columnnames = string.Join(",", Columns);
            var Records = Context.Database.SqlQuery<string>("SELECT " + columnnames + " FROM " + tablename + " ").ToList();
            return Records.ToList();
        }
    }
Posted
Updated 18-Jun-17 22:31pm
Comments
Ehsan Sajjad 17-Jun-17 13:32pm    
so the above does not works? any errors/exceptions ?
VenkataSeshu B 19-Jun-17 1:35am    
Getting this error..
"The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types."

1 solution

 
Share this answer
 
Comments
VenkataSeshu B 19-Jun-17 5:11am    
searched but did not get exact soultion
F-ES Sitecore 19-Jun-17 5:21am    
No-one else has your table so you're not going to get an "exact solution". However the first page of results have questions identical to yours only with different table names and column structures. Read the solutions to understand what the problem is and what is needed to fix it, then adapt that solution to your specific table.
VenkataSeshu B 19-Jun-17 5:27am    
if i pass single column name , table name means binding
But problem is by passing muliple columns,table name not binding records
F-ES Sitecore 19-Jun-17 5:31am    
Yes because this

Context.Database.SqlQuery<string>

is telling .net that it expects a string to be returned from the query and to store that string in "Records". When you have one column being returned that works fine. If you have two columns then you have two pieces of data, so how can you map two pieces of data (say an ID and a Name) to a single string? You need to change the return type (<string>) to something appropriate to the data being returned.

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