Russel
Russel

Reputation: 111

C# Sequence selection

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'.

Upvotes: 1

Views: 204

Answers (2)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

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 */ }

Upvotes: 7

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

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

Upvotes: 1

Related Questions