是否可以将多个值传递给动态数据源中的where参数 [英] Is it possible to pass multiple values to a whereparameter in dynamic datasource

查看:53
本文介绍了是否可以将多个值传递给动态数据源中的where参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用数据源上的whereParameter过滤gridview以显示日期范围内的所有记录,即大于等于date - 并且小于等于todate。由于whereParameters默认情况下搜索相等且不支持> =或< =,我不知道如何实现这一点。我已经尝试过以下方式执行此操作。图1.但是当变量date有多个值时,最终得到了给定的错误Fig2。



图1

I am trying to filter a gridview to show all the records within a date range i.e. greater than equal to from date -and less than equal to todate, using whereParameter on the datasource. As whereParameters by default search with equality and doesnot support >= or <=, I am at a loss how to achieve this. I have tried doing so in the following manner Fig 1. But ended up with the given error Fig2 when the variable dates has more than one value.

Fig 1.

var dates=from t1 in db.tablename
                   where (t1.datecol) >= Convert.ToDateTime(txtDate1.Text)
                   &&  (t1.datecol) >= Convert.ToDateTime(txtDate2.Text)
                   select t1.datecol;

         foreach (var items in dates)
         {
             DateTime EmailDt = Convert.ToDateTime(items);
             string EmailDate = EmailDt.ToString("MM/dd/yyyy");


             string date = Convert.ToString(EmailDate);
	     String value2 = date;
             String name2 = "Approved";
             var param2 = new Parameter();
             param2.Name = name2;
             var fkColumn = table.GetColumn(name2);
             param2.Type = Type.GetTypeCode(fkColumn.ColumnType);
             param2.DefaultValue = value2;
             GridDataSource.WhereParameters.Add(param2);
             GridDataSource.AutoGenerateWhereClause = true;
         }



图2.


Fig 2.

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: No property or field 'datecol1' exists in type 'tablename'.





请告知是否可以使用多值,或者只能将一个值传递给此whereparameter。提前完成。



Please advise if multivalue is possible or only one value can passed to this whereparameter. Thanx in advance.

推荐答案

尝试使用lambda表达式



try using lambda expression

dbContext.tablename.Where(x => x.dateColumn >= startDate && x.dateColumn <= endDate);


这篇关于是否可以将多个值传递给动态数据源中的where参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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