8

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();
4

4 回答 4

9

尝试这个 :

var query = from c in customers where c.CustomerID.Equals(txtSerchId.Text) select c;
于 2012-05-04T12:07:26.783 回答
0

简单点:

  1. 对于平等:

    var result = from c in customers where c.CustomerID ==Convert.ToInt32(txtSerchId.Text) select c;

  2. 对于更大的:where c.CustomerID >= Convert.ToInt32(txtSerchId.Text)

  3. 少 : where c.CustomerID <= Convert.ToInt32(txtSerchId.Text)

于 2013-10-26T07:09:21.193 回答
0

引用您的评论“我正在将用户输入的值与我拥有的对象集合进行比较,以搜索 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);

请注意,这是非常低效的,因为它首先从数据库中提取所有记录,然后根据您的字符串比较过滤它们。但老实说,我不知道更好的方法,所以这可能值得一试。

于 2012-05-04T12:15:46.597 回答
0
 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

*这对我有用

于 2017-10-25T11:06:13.280 回答