tags:

views:

95

answers:

2

Just I want to find any of the intColl of CustomerData is of single digit in length and select that 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 }
                             }
          );

when executing the following

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

I receive

Cannot implicitly convert type 'int' to 'bool'.

+7  A: 

While the query you've written should be written as,

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

I think, based on your question text, you should be doing:

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

And to return the actual object (per the comment):

var query = cdata.FirstOrDefault(d => d.intColl.Any(c => c < 10 && c >= 0));
if (query == null) { /* nothing fits the criteria */ }
else { /* use `query` object */ }
Mehrdad Afshari
I think the guy wants the actual CustomerData object returned, not a boolean value. Though his text is barely decodable. So I'm guessing the first Any should be a Where, and maybe a Take(1) at the end...?
KernelJ
First rather than Take(1).
KernelJ
+1  A: 

Not sure I follow you. It could be this:

var query = cdata.Where(cd => cd.intColl.Any(i => i<10)); //assuming no negatives

or this:

var query = cdata.Where(cd => cd.intColl.Length < 10);
Joel Coehoorn