[英]Very simple LINQ query, passing in textbox value to the where clause
I want my LINQ query to count the number of instances a member is present in a table based on what the user enters into a textbox. 我希望我的LINQ查询能够根据用户在文本框中输入的内容来统计表中成员的实例数。
As a result, I have the following 结果,我有以下
protected void btnSubmit_Click(object sender, EventArgs e)
{
LoginDataContext lg = new LoginDataContext();
int logincheck = (from r in lg.tblOnlineReportingLogins
where r.MemberID == tbxMember.Text.ToString()
select r).Count();
When I debug this, the value appears as 0 when I know this is not the case. 当我调试它时,当我知道不是这种情况时,该值显示为0。
Can someone point out where I am going wrong? 有人可以指出我要去哪里了吗?
I suppose the MemberId
property is an integer. 我想
MemberId
属性是一个整数。 So you cannot compare it against a string value. 因此,您无法将其与字符串值进行比较。
Text.ToString()
is redundant anyway, since Text
is already a string. Text.ToString()
仍然是多余的,因为Text
已经是一个字符串。
where r.MemberId.ToString() == tbxMember.Text
should work. 应该管用。
A cleaner solution would be to parse the Text
property into an int
value (together with some validity checking) and do the comparison using int
s, like 较干净的解决方案是将
Text
属性解析为一个int
值(以及一些有效性检查),并使用int
进行比较,例如
int targetId;
if (Int32.TryParse(tbxMember.Text, out targetId)) {
int logincheck = (from r in lg.tblOnlineReportingLogins
where r.MemberID == tbxMember.Text.ToString()
select r).Count();
// ...
} else {
MessageBox.ShowMessage("Invalid Id");
}
EDIT : Sorry, perhaps I was too quick, I assumed MemberId
was an integer. 编辑 :对不起,也许我太快了,我认为
MemberId
是一个整数。 But in this case, your code shouldn't even have compiled. 但是在这种情况下,您的代码甚至都不应该编译。 I'm just wondering about the upvotes :-) ...
我只是想知道赞成票:-) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.