如何在C#MVC3中执行字符串格式的LINQ查询 [英] How to Execute String formatted LINQ query in c# MVC3

查看:68
本文介绍了如何在C#MVC3中执行字符串格式的LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     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);
        }

我如何在C#中执行此LINQ查询,我只想运行查询并获取结果..

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

请帮助我...

预先感谢...

推荐答案

请勿执行此操作.改用谓词生成器!

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

简而言之,这是C#的一个很好的实现: http://www.albahari.com/nutshell/predicatebuilder.aspx

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

谓词生成器使用一个名为linqkit的免费库: http://www.albahari.com/nutshell/linqkit.aspx

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

这里是另一个SO问题,解决了这个主题以及整个过程是如何工作的 PredicateBuilder如何工作

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

这篇关于如何在C#MVC3中执行字符串格式的LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆