2

I try to use where clause in my LINQ queries. When I use numerical value, it's fine but when I want to compare string value, it gives an error in where clause.

string StartBarcode = (from s in datamodel.Packages
                                    where s.RealBarcode == SelectedBarcode
                                    select s.StartBarcode).FirstOrDefault().ToString();

IQueryable<PageData> q = (from v in datamodel.VW_WaypointDetails
                                          where (v.RealBarcode == null) ? StartBarcode : v.RealBarcode
                                          select new PageData
                                          {
                                              Name = v.Name,
                                              Surname = v.Surname,
                                              Description = v.Description
                                           }

Errors are that 'Cannot convert lambda expression to type 'string' because it is not a delegate type' (I added System.Linq) and Cannot implicitly convert type 'string' to 'bool'. How can I use that switch-case statement with string value? Thanks for all answers.

nevra
  • 418
  • 1
  • 7
  • 21
  • In your `where (v.RealBarcode == null) ? StartBarcode : v.RealBarcode` the `the expression which evaluates to the bool is expected`. What happens here is if `v.RealBarcode is not null ` then it returns string which should be bool evaluted value – Mahesh Feb 19 '15 at 08:38

2 Answers2

4

Sure it will give you an error, cause LINQ where clause expects logic where result should be true or false.

Your logic is (v.RealBarcode == null) ? StartBarcode : v.RealBarcode which returns just string.

If we translate your linq to English it will sounds like:

Select some values where Barcode

It should sound like:

Select some values where Barcode is equal to something

I think your logic should look something like this

where v.RealBarcode == (v.RealBarcode == null) ? StartBarcode : v.RealBarcode
Michael Samteladze
  • 1,310
  • 15
  • 38
4
where (v.RealBarcode == null) ? StartBarcode : v.RealBarcode

will fail miserably because this should be a predicate but you can do this like this:

IQueryAble<PageData> q = datamodel.VW_WaypointDetails.AsEnumerable()
                         .Select(wpd=> new PageData 
                                         {
                                            Name = wpd.Name,
                                            SurName = wpd.Surname,
                                            Description = wpd.Description, 
                                            Barcode = wpd.Barcode == null ? StartBarCode : wpd.Barcode
                                            }).AsQueryAble();

Sorry forgot the call to .AsQueryAble(). Not sure if this works to be honest but you can try to leave out the call to .AsEnumerable() (which is actually getting the data from the database) ... I know though that Linq to Entities does not support certain features of the normal Linq Scope so this may be not possible and you would have to create the actual objects later.

Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28
  • I understand how to use where clause, thank you. but when i try to write like that it gives _incorrect select usage_ error. I have to use IQueryable types.. – nevra Feb 19 '15 at 09:07