0
     public ActionResult GenerateReport(FormCollection Form)
        {
            var type_id = Form["type_id"];           
            var Start_Date = Form["Start_Date"];

            StringBuilder sb = new StringBuilder();          
            sb.Append("db.contracts.Where(");
            if (!type_id.Equals(null))
            {
                sb.Append("a => a.type_id == type_id");
            }

            if (!Start_Date.Equals(null))
            {
                sb.Append("&& a => a.Start_Date == Start_Date");
            }
            sb.Append(");");
           //here I need to run the query!!
            return View(cm);
        }

How can i execute this LINQ query in C#, I just want to run the query and get the results..

Please help me...

Thanks in Advance...

Mukarram
  • 115
  • 4
  • 14
  • 4
    I'm interested in why you are doing this? – George Johnston May 28 '13 at 14:29
  • After you edit: you really only want to use [Dynamic Linq](http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx). – Gert Arnold May 28 '13 at 14:56
  • @GertArnold, you can compile strings using the C# CodeDom classes at runtime, you dont have to emit things.. See GekaLlaur's answer for reference (too many edits, my answers irrelevant now too =() – Dead.Rabit May 28 '13 at 14:57
  • @Dead.Rabit Yeah, all right, it's probably overkill either way unless this is some kind of special assignment. But I think the OP is only looking for a way to add predicates dynamically because the data source part of the query is know at run time (apparently). (BTW I removed my previous comment just before yours :) ) – Gert Arnold May 28 '13 at 15:01
  • GertArnold I think you're right, though I have implemented this feature a few times @DimitarDimitrov though mostly in developer tools (internal log pages for example) – Dead.Rabit May 28 '13 at 15:07

2 Answers2

1

EDIT: this answer works with the original question content, how to execute this LINQ expression from a string:

var expression = @"( from a in Assignments
    where a.assignmentID == 1 && a.assignmentID == 4 )
    select new AssignmentModel
    {
        Title = a.Title,
        Description = a.Description
    }).ToList()";

though this answer should work with the generated string in the current question, only you would be injecting db.contracts rather than Assignments as I explain below.


The alternative to using the compilation classes as @GekaLlaur suggests is to compile the string into an Expression tree, which is possibly more appropriate here as you're compiling LINQ expressions.

The solution found in this SO post (Chosen Solution) looks as though it should work out of the box for you, only using var p = Expression.Parameter(typeof(List<AssignmentModel>), "Assignments"); to describe your parameter. I would test it with your specific example but I don't already have the libraries (linked here).

Community
  • 1
  • 1
Dead.Rabit
  • 1,965
  • 1
  • 28
  • 46
  • Just an added note, compiling code (any code) from strings at runtime is the definition of a security hole.. be very very careful to ensure that any parsed strings meet your expectations and can't run riot on the host PC. – Dead.Rabit Oct 22 '14 at 09:45
1

Don't do this. Use a Predicate Builder instead!

Here is a great implementation from C# in a nutshell: http://www.albahari.com/nutshell/predicatebuilder.aspx

The predicate builder uses a free library called linqkit: http://www.albahari.com/nutshell/linqkit.aspx

Here is another SO question that addresses this topic and how the whole thing works How does PredicateBuilder work

Community
  • 1
  • 1
amhed
  • 3,649
  • 2
  • 31
  • 56