C# 如何在 Linq 查询中比较字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10448451/
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
How to Compare strings in Linq Query
提问by MBasit
CompareTo is not working here for me.
CompareTo 在这里对我不起作用。
My linq query is
我的 linq 查询是
var result = from c in customers
where c.CustomerID.CompareTo(txtSerchId.Text) >= 0
select` c;
and em getting an exception
和他们得到一个例外
//////EXCEPTION///////////
//////例外///////////
System.ArgumentException was caught
Message=Value does not fall within the expected range.
My code is something like this
我的代码是这样的
var result =
from c in customers
where c.CustomerID.CompareTo(txtSerchId.Text) >= 0
select c;
if (result != null)
{
IEnumerator<Customer> resultEnum = result.GetEnumerator();
while (resultEnum.MoveNext())
{
Customer c = (Customer)resultEnum.Current;
addToDataSet(Guid.NewGuid().ToString(), c);
}
ShowResult();
}
else
{
MessageBox.Show("No Customer found within criteria");
}
exception is at this line
例外是在这一行
IEnumerator<Customer> resultEnum = result.GetEnumerator();
回答by Ovais Khatri
try this :
尝试这个 :
var query = from c in customers where c.CustomerID.Equals(txtSerchId.Text) select c;
回答by Bazzz
Quoting from your comment "i am comparing user entered value to collection of objects i have, to search Customers having IDs less than or you can say greater than that entered by user."
引用您的评论“我正在将用户输入的值与我拥有的对象集合进行比较,以搜索 ID 小于或您可以说大于用户输入的客户。”
try this for "greater than":
试试这个“大于”:
int customerId = int.Parse(txtSerchId.Text);
if (customerId > 0)
{
var result = from c in customers where c.CustomerID > customerId select c;
}
int customerId = int.Parse(txtSerchId.Text);
if (customerId > 0)
{
var result = from c in customers where c.CustomerID > customerId select c;
}
Update as more infomation has been added in comments:
在评论中添加更多信息时进行更新:
Try this:
尝试这个:
customers.ToList().Where(c => c.CustomerID.CompareTo(txtSerchId.Text) >= 0);
Notice that this is vastly inefficient as it first pulls ALL records from the database and THEN filters them according to your string comparison. But to be honest I don't know a better way so this might be worth a try.
请注意,这非常低效,因为它首先从数据库中提取所有记录,然后根据您的字符串比较过滤它们。但说实话,我不知道更好的方法,所以这可能值得一试。
回答by Bazzz
Go simple:
简单点:
For Equality:
var result = from c in customers where c.CustomerID ==Convert.ToInt32(txtSerchId.Text) select c;For Greater :
where c.CustomerID >= Convert.ToInt32(txtSerchId.Text)For less :
where c.CustomerID <= Convert.ToInt32(txtSerchId.Text)
为了平等:
var result = from c in customers where c.CustomerID ==Convert.ToInt32(txtSerchId.Text) select c;对于更大:
where c.CustomerID >= Convert.ToInt32(txtSerchId.Text)少 :
where c.CustomerID <= Convert.ToInt32(txtSerchId.Text)
回答by Praveen Kumar Rejeti
var List = (from t in ObjCon.TableName
where t.GameDate.Value.CompareTo(GameDate) >= 0
join t1 in ObjCon.Teams on t.Home equals t1.TeamId
where t1.SportId == 3
*This Worked FOR ME
*这对我有用

