[英]How to Compare strings in Linq Query
CompareTo不適合我。
我的linq查詢是
var result = from c in customers
where c.CustomerID.CompareTo(txtSerchId.Text) >= 0
select` c;
而他們得到一個例外
//////例外///////////
System.ArgumentException was caught
Message=Value does not fall within the expected range.
我的代碼是這樣的
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");
}
異常就在這條線上
IEnumerator<Customer> resultEnum = result.GetEnumerator();
試試這個 :
var query = from c in customers where c.CustomerID.Equals(txtSerchId.Text) select c;
引用您的評論“我將用戶輸入的值與我擁有的對象集合進行比較,以搜索ID低於或者您可以說比用戶輸入的ID更高的客戶。”
試試這個“大於”:
int customerId = int.Parse(txtSerchId.Text); if (customerId > 0) { var result = from c in customers where c.CustomerID > customerId select c; }
更新為評論中添加了更多信息:
試試這個:
customers.ToList().Where(c => c.CustomerID.CompareTo(txtSerchId.Text) >= 0);
請注意,這是非常低效的,因為它首先從數據庫中提取所有記錄,然后根據您的字符串比較過濾它們。 但說實話,我不知道更好的方法,所以這可能值得一試。
簡單:
對於平等:
var result = from c in customers where c.CustomerID ==Convert.ToInt32(txtSerchId.Text) select c;
對於Greater: where c.CustomerID >= Convert.ToInt32(txtSerchId.Text)
for less: where c.CustomerID <= Convert.ToInt32(txtSerchId.Text)
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
*這適用於我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.