简体   繁体   English

为什么我不能在C#中比较两个ID

[英]why cant I compare two id's in c#

I tried to compare two ids and get some result.it works for other strings.but not for this. 我试图比较两个id并得到一些结果。它适用于其他字符串。但不适用于此。 I tried like this. 我尝试过这样。

var neededData = mainFaires.Where(c => c.trimacid == passId );

in here passId= OX20160330HAVHAV 在这里passId= OX20160330HAVHAV

and in the mainFaires list, in somewhere it includes this id .but it didn't give the result.I found in here mainFaires列表中的某个位置包含此id ,但未给出结果。我在这里找到

var x = mainFaires.ElementAt(27261);

this list include the same id .but didn't give result.I can't think why. 该列表包含相同的id ,但未给出结果。我不知道为什么。

ElementAt is find the position. ElementAt查找位置。

You should use select to find the records 您应该使用select查找记录

var x = mainFaires.Select(o => o.trimacid == 27261);

You should use .ToList() .First() or .FirstOrDefault() to actually commit the query and get a result. 您应该使用.ToList() .First() .ToList().FirstOrDefault()来实际提交查询并获得结果。 Your code only defined the query, but didn't actually submit it to the data collection. 您的代码仅定义了查询,但并未实际将其提交到数据集合。

If you expect only one item as a result, you're code should look like this: 如果您只希望得到一项结果,那么您的代码应如下所示:

var neededData = mainFaires.Where(c => c.trimacid == passId ).FirstOrDefault();

If there was no item found, neededData would be NULL or whatever the default value is. 如果未找到任何项目,则requiredData将为NULL或任何默认值。 You may also check the Documentation here https://msdn.microsoft.com/en-us/library/system.linq.enumerable%28v=vs.100%29.aspx 您也可以在这里查看文档https://msdn.microsoft.com/zh-cn/library/system.linq.enumerable%28v=vs.100%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM