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中,我有以下DataSource设置,我试图在后面的代码中动态分配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...
这给了我一个错误')'或','预期。我也试过把它放在引号内,因为没有把它作为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.
这使得运算符'=='与操作数类型'DateTime'和'String'不兼容。我已经尝试了其他几种方法,但我显然在这里遗漏了一些东西。
Similar code is working fine for strings.
类似的代码适用于字符串。
6 个解决方案
#1
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()...
#2
I believe you need to include double quotes around the string being converted to a DateTime.
我相信你需要在被转换为DateTime的字符串周围加上双引号。
LinqDataSource1.Where = "MyDateColumn == DateTime(\"" + DateTime.Now.ToString() + "\")";
#3
LinqDataSource1.Where = "MyDateColumn == Convert.ToDateTime(\"" + DateTime.Now + "\")";
LinqDataSource1.Where =“MyDateColumn == Convert.ToDateTime(\”“+ DateTime.Now +”\“)”;
#4
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>
#5
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) + @""")";
#6
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);
#1
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()...
#2
I believe you need to include double quotes around the string being converted to a DateTime.
我相信你需要在被转换为DateTime的字符串周围加上双引号。
LinqDataSource1.Where = "MyDateColumn == DateTime(\"" + DateTime.Now.ToString() + "\")";
#3
LinqDataSource1.Where = "MyDateColumn == Convert.ToDateTime(\"" + DateTime.Now + "\")";
LinqDataSource1.Where =“MyDateColumn == Convert.ToDateTime(\”“+ DateTime.Now +”\“)”;
#4
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>
#5
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) + @""")";
#6
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);