C# 使用 DateTime 列设置 LinqDataSource Where 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1051255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the LinqDataSource Where Clause using DateTime Column
提问by BrianG
In C#.net, I have the following DataSource setup that I am trying to dynamically assign a WHERE clause to in the code behind...
在 C#.net 中,我有以下数据源设置,我试图在后面的代码中动态分配一个 WHERE 子句......
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="MyNameSpace.DataClasses1DataContext"
TableName="MyTableWithADateTimeColumn" >
</asp:LinqDataSource>
The code behind looks something like this...
后面的代码看起来像这样......
LinqDataSource1.Where = "MyDateColumn == DateTime(" + DateTime.Now + ")";
This gives me an error of ')' or ',' expected
. I've also tried casting it inside quotation marks, as well, as without casting it as DateTime and with quotation marks...
这给了我一个错误')' or ',' expected
。我也试过将它转换为引号,因为没有将它转换为 DateTime 并且使用引号......
LinqDataSource1.Where = @"MyDateColumn == """ + DateTime.Now + @""" ";
This gives me Operator '==' incompatible with operand types 'DateTime' and 'String'
. I've tried several other ways, but I am obviously missing something here.
这给了我Operator '==' incompatible with operand types 'DateTime' and 'String'
。我尝试了其他几种方法,但显然我在这里遗漏了一些东西。
Similar code is working fine for strings.
类似的代码适用于字符串。
采纳答案by bytebender
is it this? What about this then...
是这个吗?那这怎么办……
LinqDataSource1.Where = "MyDateColumn == DateTime.Parse(" + DateTime.Now + ")";
//can't create a date from string in constructor use .Parse()...
回答by Reed Copsey
I believe you need to include double quotes around the string being converted to a DateTime.
我相信您需要在转换为 DateTime 的字符串周围包含双引号。
LinqDataSource1.Where = "MyDateColumn == DateTime(\"" + DateTime.Now.ToString() + "\")";
回答by BrianG
So the final solution as suggested by J.13.L looked like this...
所以 J.13.L 建议的最终解决方案看起来像这样......
LinqDataSource1.Where = @"MyDateColumn == DateTime.Parse(""" + MyDateTime + @""") ";
But since I didn't want to match on the time part of the date it really looked more like this...
但是由于我不想匹配日期的时间部分,所以它看起来更像是这样......
LinqDataSource1.Where = @"MyDateColumn >= DateTime.Parse(""" + MyDateTime + @""") AND MyDateColumn < DateTime.Parse(""" + MyDateTime.AddDays(1) + @""")";
回答by BrianG
Another programmatically way:
另一种编程方式:
dataSource.WherePredicateParameters.Clear();
OrExpressionParameter expression = new OrExpressionParameter();
expression.Parameters.Add("Birthday", DbType.DateTime, Convert.ToDateTime(txtBirthday.Text).ToString());
dataSource.WherePredicateParameters.Add(expression);
回答by Venugopal
LinqDataSource1.Where = "MyDateColumn == Convert.ToDateTime(\"" + DateTime.Now + "\")";
LinqDataSource1.Where = "MyDateColumn == Convert.ToDateTime(\"" + DateTime.Now + "\")";
回答by Sanjay Gupta
It's simple and straight forward:
这很简单直接:
Look in the page source of "asp:LinqDataSource" and add that clause in the "where" section.
查看“asp:LinqDataSource”的页面源代码并在“where”部分添加该子句。
Adding this through the wizard with a NULL parameter fails.
通过向导添加 NULL 参数失败。
Here is an example:
下面是一个例子:
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="MyDataContext"
EntityTypeName="" GroupBy="MyItem" Select="new (key as Item1, Count() as TotalQuantity)"
TableName="MyTable"
Where="Country == @Country && DateProcessed == NULL">
<WhereParameters>
<asp:ControlParameter ControlID="ddlCountry" DefaultValue="US"
Name="Country" PropertyName="SelectedValue" Type="String" />
</WhereParameters>
</asp:LinqDataSource>