C# 序列选择

标签 c# linq

只是我想找到任何 CustomerDataintColl 的长度是个位数,然后选择那个 customerData。

List<CustomerData> cdata = new List<CustomerData>();

cdata.Add(
           new CustomerData { Key = 1, Name = "Marc",
                              intColl = new List<int>() { 1, 2, 3 }
                             }
         );


cdata.Add(
           new CustomerData { Key = 2, Name = "Eric",
                             intColl = new List<int>() { 11, 12, 13 }
                            }
         );


cdata.Add(
           new CustomerData { Key = 3, Name = "Peter", 
                              intColl = new List<int>() { 111, 112, 113 }
                             }
         );  


cdata.Add(
            new CustomerData { Key = 4, Name = "Peter",
                               intColl = new List<int>() { 1111, 1112, 1113 }
                             }
          );

执行以下操作时

var query = cdata.Any(d => d.intColl.Find(c => c == 1));

我收到了

无法将类型“int”隐式转换为“bool”。

最佳答案

虽然您编写的查询应该写成,

var query = cdata.Any(d => d.intColl.Contains(1));

我认为,根据您的问题文本,您应该这样做:

var query = cdata.Any(d => d.intColl.Any(c => c < 10 && c >= 0));

并返回实际对象(根据评论):

var query = cdata.FirstOrDefault(d => d.intColl.Any(c => c < 10 && c >= 0));
if (query == null) { /* nothing fits the criteria */ }
else { /* use `query` object */ }

关于C# 序列选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1903677/

相关文章:

c# - Task.Run 如何避免异步代码中的死锁?

c# - 我可以阻止 Uri 在 WebRequest.Create 中取消对 url 的编码吗?

c# - Mongodb C# 查找异步。使用linq过滤文档内的列表

c# - Linq 查询从多个 List<string> 中选择单个字符串

c# - 如何将两个表格放在一起?

c# - VBProjectsEvents 在哪里?

c# - 如何在winforms中设置输入密码的文本框?

linq - Entity Framework 表之间的多重关系

C#:Func<> 而不是方法?

Linq group by + where 对于每个组