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...

5
  • 4
    I'm interested in why you are doing this? Commented May 28, 2013 at 14:29
  • After you edit: you really only want to use Dynamic Linq. Commented May 28, 2013 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 =() Commented May 28, 2013 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 :) ) Commented May 28, 2013 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) Commented May 28, 2013 at 15:07

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.