Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code:
var ddt1 = dt.AsEnumerable()
     .GroupBy(r => r.Field<int>("ChartId"))
     .Select(g => new
     {
         ChartId = g.Key,
         SumData = g.Sum(r => r.Field<int>("YAxisData"))
     })
     .ToList();


I want to do some thing like this:
string funcVariable="  .GroupBy(r => r.Field<int>("ChartId"))
                     .Select(g => new
                     {
                         ChartId = g.Key,
                         SumData = g.Sum(r => r.Field<int>("YAxisData"))
                     })"
                var ddt1 = dt.AsEnumerable()
                   funcVariable
                     .ToList();


Please suggest

What I have tried:

if i use a variable, then variable tolist is considered as characters to list which is not correct. Please suggest the correct way.
Posted
Updated 15-Mar-17 2:27am
Comments
Karthik_Mahalingam 15-Mar-17 7:35am    
Not possible.
Prateek Dalbehera 15-Mar-17 7:48am    
Have not think of such but a nice idea, like "execute" used in sql server for dynamic sql queries. As of now, its not supported. MS should start researching on it!!!!

You can't do that directly with C# - the language does not support it.

You can do it; you can create code, compile, and run it from inside an app: How to programmatically compile code using C# compiler[^] but it's not a good solution for general purpose work - it's goign to be hard to maintain, hard to control, and unless you are very careful it effectively hands control of your application internals to the user.

I can't think of a good reason to do that that putting the Linq code in a method instead wouldn't be a better solution.
 
Share this answer
 
v2
It is possible using Expression Class (System.Linq.Expressions)[^] however it is outside the scope of Q&A.

This article may help: Basics of LINQ & Lamda Expressions - Code Project[^]
 
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