DotnetSparrow
DotnetSparrow

Reputation: 27996

Cannot implicitly convert type 'string' to 'bool' linq query

I have this LINQ query:

 var query = from row in context.data_vault
             group row by row.STATE into g
             select new {
                 State = g.Key,
                 Count = g.Sum(row => row.SCORE),
                 Phones = g.Count(c => c.PHONE)
             };

When executed, I get following error:

Cannot implicitly convert type 'string' to 'bool' at g.Count(c => c.PHONE)

Upvotes: 1

Views: 4253

Answers (3)

Richard
Richard

Reputation: 30628

Count() is expecting a predicate - something that returns a boolean. You've got it returning a string. I'm not sure what you're after, but if you wanted to count the number of non-blank phones, you would modify it to be

 var query = from row in context.data_vault
             group row by row.STATE into g
             select new {
                 State = g.Key,
                 Count = g.Sum(row => row.SCORE),
                 Phones = g.Count(c => c.PHONE != "")
             };

Upvotes: 0

Ove
Ove

Reputation: 6317

What are you trying to count? If you want to count non-null or non-empty phone numbers, you need to use g.Count(c => String.IsNullOrEmpty(c.PHONE)==false )

Upvotes: 0

sloth
sloth

Reputation: 101142

The Count method expects a function returning a boolean value. It looks like your PHONE property is a string, so this will not work.

What exactly are you trying to count?

Maybe you want all non-empty phone numbers or anything like that, try

g.Count(c => !String.IsNullOrWhitespace(c.PHONE))

Upvotes: 1

Related Questions