如何使用system.linq.dynamic构建动态where子句 [英] How to use system.linq.dynamic to build dynamic where clause

查看:364
本文介绍了如何使用system.linq.dynamic构建动态where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们怎么了?



我们正在研究我公司的一个项目(C#& ASP.Net),在其中一个页面中,有一个机制可以提取一些数据从使用WCF的表格。



到目前为止很好!



数据存储在DTO中class(让我们将其命名为Tikun94ActualPerformanceSummaryDTO)并最终将其转换为IEnumerable对象:

Hey guys what's up?

We are now working on a project in my company (C # & ASP.Net) and in one of the pages, there is a mechanism which pulled some data from a table using WCF.

So far So good!

that data is stored inside a DTO class (let's name it Tikun94ActualPerformanceSummaryDTO) and eventually i converts it to an IEnumerable object like that:

IEnumerable<Tikun94ActualPerformanceSummaryDTO> filtered = this.Tikun94Summaries as IEnumerable<Tikun94ActualPerformanceSummaryDTO>;



(Tikun94Summaries是Tikun94ActualPerformanceSummaryDTO类型的对象)。



现在,这些数据通过页面上的表格查看,用户可以使用我给他的一些选项来过滤表格。

按下提交按钮,我想迭代通过现有的过滤(我之前创建的IEnumerable类型)并根据用户给出的值对其进行过滤。



对于用户提供的每个值,我需要设置一个一次又一次过滤过滤的Where子句。现在,因为我不想在同一个IEnumerable过滤上一遍又一遍地查询,我想构建Where动态

并且只运行一次。



让我们看一些代码:


(Tikun94Summaries is an object of type Tikun94ActualPerformanceSummaryDTO).

Now, These data are viewed through a table on the page and the user can filter the table using a few options that I give him.
While pressing the 'Submit' button, i want to iterate through the existing "filtered" (of type IEnumerable which I created before) and filter it by the values given me from the user.

For each value given from the user, I need to set a Where clause that filter "filtered" again and again. Now, because I do not want to query over and over again on the same IEnumerable "filtered" , I want to build the Where dynamically
and than run it only one time.

Lets see some code:

IEnumerable<Tikun94ActualPerformanceSummaryDTO> filtered = this.Tikun94Summaries as IEnumerable<Tikun94ActualPerformanceSummaryDTO>;
            IQueryable<Tikun94ActualPerformanceSummaryDTO> queryableData = filtered.AsQueryable();



这里(上图),正如您所看到的,我首先将IEnumerable对象转换为IQueryable对象,以便执行以下操作:




Here (above), as you can see, i start by converting the IEnumerable object into IQueryable object in order to do the following:

string query = "filtered.Where(";



现在,我开始检查用户给出的值,并使用Switch Case设置Where根据它们的位置(让我们以两个案例为例):


Now, i start checking the values given from the user and set the Where according to them using Switch Case (Let's take 2 cases as an example):

#region OperatorLineId

switch (cmbLine.SelectedValue)
            {
                case "0":
                    query += += "u => u.OperatorLineId == u.OperatorLineId  ";
                    break;
                default:
                    query += "u => u.OperatorLineId == Convert.ToInt32(cmbLine.SelectedValue) ";
                    break;
            }
#endregion

#region ExclusivityLine 
switch (ddlExclusivityLine.SelectedValue)
            {
                case "0":
                    query += "AND u => u.ExclusivityLine == u.ExclusivityLine ";
                    break;
                default:
                    query += "AND u => u.ExclusivityLine == ddlExclusivityLine.SelectedValue )";
                    break;
            }
            #endregion

var externals = new Dictionary<string, object>();
            externals.Add("filtered", queryableData);



这是我得到EXECPTION的地方:-(:


And this is where i get the EXECPTION :-( :

var expression = System.Linq.Dynamic.DynamicExpression.Parse(typeof(IQueryable<Tikun94ActualPerformanceSummaryDTO>), query, new[] {externals});



例外消息:

类型'Tikun94ActualPerformanceSummaryDTO'中没有属性或字段'u''



我对我做错了几点猜测。也许我我试图使用lambda表达式,我不应该这样,也许我米未访问DTO里面的物品cuurectly。锄头我可以访问DTO内的值吗?也许我不认为?



最后,我想动态构建Where子句(在某些交换机情况下我必须使用operator'>'或'<')。



我想在写这个算法时我有多个错误,我想知道写这个操作的正确语法是什么。



这是Tikun94ActualPerformanceSummaryDTO类(当然还有更多的参数,但这里仅举例):


Exception message:
"No property or field 'u' exists in type 'Tikun94ActualPerformanceSummaryDTO'"

I have a few guesses as to what I was doing wrong. Maybe i'm trying to use lambda expression where I'm not supposed to and maybe i'm not accessing the items inside the DTO cuurectly. Hoe can i access the values inside the DTO? maybe i'm not suppose to?

Eventually, i want to build that Where clause dynamically (in some of the switch cases i have to use operator '>' or '<').

I guess I have more than one error in writing this algorithm and I'd love to know what the correct syntax for writing this operation.

This is the Tikun94ActualPerformanceSummaryDTO class (There are of course many more parameters there, but here it's just for example):

using System;
using WcfSerialization = global::System.Runtime.Serialization;

namespace OpsReporting.DataContracts
{
    [WcfSerialization::DataContract(Namespace = "urn:OperatorsReporting.ServiceContracts", Name = "Tikun94ActualPerformanceParamDTO")]
	public partial class Tikun94ActualPerformanceSummaryDTO
    {
        public int operatorLineId;
        public string exclusivityLine;
        
        [WcfSerialization::DataMember(Name = "OperatorLineId", IsRequired = true, Order = 6)]
        public int OperatorLineId
        {
            get { return operatorLineId; }
            set { operatorLineId = value; }
        }

        [WcfSerialization::DataMember(Name = "ExclusivityLine", IsRequired = true, Order = 8)]
        public string ExclusivityLine
        {
            get { return exclusivityLine; }
            set { exclusivityLine = value; }
        }    
}







非常感谢!



我尝试过:



很多:见上文




Thank you very much!

What I have tried:

Lots: see above

推荐答案

在动态linq中,传入的字符串参数只是普通的sql,而不是任何lambda表达式,所以你的条件应该是这样的:



In dynamic linq, the string parameter passed in is just normal sql, not any lambda expressions, so your condition should be like:

 case "0":
       query += "OperatorLineId = OperatorLineId  ";
       break;
default:
      query += string.Format("OperatorLineId = {0}",Convert.ToInt32(cmbLine.SelectedValue));

var result = filtered.Where(query);





你可能想要阅读这篇文章更多。或考虑查看本文关于使用WCF动态Linq到实体查询



you might want to read this article for more. or consider checking out this article about dynamic Linq to Entities Queries using WCF


这篇关于如何使用system.linq.dynamic构建动态where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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