0

I am using LINQ and I want to call a method (e.g. checkOpenClose) by passing parameters, and have it return true/false:

var model = (from account in _ctx.Account
             where (...)
             select new DetailVM
             {
               Id = account.Id,
               Name = account.CompanyName,
               OpenNow = checkOpenClose(account.MonOpen, account.MonClosed,           
                           account.TueOpen, account.TueClosed,
                           account.WedOpen, account.WedClosed,
                           account.ThuOpen, account.ThuClosed,
                           account.FriOpen, account.FriClosed,
                           account.SatOpen, account.SatClosed,
                           account.SunOpen, account.SunClosed)
             }).Tolist()

I get the following error:

LINQ to Entities does not recognize the method 'Boolean checkOpenClose (System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String)' method, and this method cannot be translated into a store expression.

What am I doing wrong?

AlBlue
  • 23,254
  • 14
  • 71
  • 91
Ben
  • 375
  • 8
  • 32

3 Answers3

0

Remember that query is going to be translated to sql, so your Linq provider can't translate your method call to something valid. If you really need to call that method then I suggest call AsEnumerable extension method as I show below:

   var model=    (from account in _ctx.Account
                  where (...)
                  select account)
                 .AsEnumerable()
                 .Select(account=> new DetailVM
                                   {
                                      Id = account.Id,
                                      Name = account.CompanyName,
                                      OpenNow = checkOpenClose(account.MonOpen, account.MonClosed,
                                                               account.TueOpen, account.TueClosed,
                                                               account.WedOpen, account.WedClosed,
                                                               account.ThuOpen, account.ThuClosed,
                                                               account.FriOpen, account.FriClosed,
                                                               account.SatOpen, account.SatClosed,
                                                               account.SunOpen, account.SunClosed)


                                    })
                .Tolist();

That will allow you to execute the projection what you need using Linq to Objects

ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • This worked! Thank you! When using .AsEnumerable() it executes the DB (from account in _ctx.Account where (...) select account) and then .Select(....).ToList() grabs the values from the memory? Trying understand what .AsEnumerable() does and when it should be used. pls correct me if I am wrong. – Ben Jun 24 '16 at 19:56
  • What it is before `AsEnumerable` call is going to be execute in your DB. The main difference between `ToList` and `AsEnumerable` is that `AsEnumerable` maintains [deferred execution](https://blogs.msdn.microsoft.com/charlie/2007/12/10/linq-and-deferred-execution/).More info about your questions can be found in this [post](http://stackoverflow.com/questions/5311034/what-is-the-effect-of-asenumerable-on-a-linq-entity) – ocuenca Jun 24 '16 at 20:00
0

As I see you are using entity framework datacontext which will convert linq statements to sql scripts so when you use a method it cannot convert it to sql. Try this

var model= (from account in _ctx.Account
                      where (...)
                      select new {
                          Id = account.Id,
                          Name = account.CompanyName,
                          MO = account.MonOpen, 
                          MC = account.MonClosed,
                          TUO = account.TueOpen, 
                          TUC = account.TueClosed,
                          WO = account.WedOpen,
                          WC = account.WedClosed,
                          THO = account.ThuOpen, 
                          THC = account.ThuClosed,
                          FO = account.FriOpen,
                          FC = account.FriClosed,
                          SO = account.SatOpen,
                          SC = account.SatClosed,
                          SUO = account.SunOpen,
                          SUC = account.SunClosed

                          }).Tolist().select(m=> new DetailVM {

                        Id = account.Id,
                        Name = account.CompanyName,
                        OpenNow = checkOpenClose(MO,MC,TUO,TUC,WO,WC,THO,THC,FO,FC,SO,SC,SUO,SUC)

            }).ToList();
Krishna
  • 1,945
  • 1
  • 13
  • 24
  • I tried your way, it does not recognize account,FO attributes http://prntscr.com/bklwr6 – Ben Jun 24 '16 at 19:35
-1

You have expressions within your Boolean method that have no SQL counterpart...this has been fleshed out on SO a few times see

EF needs expressions that have an SQL equivalent

or

another from SO

Community
  • 1
  • 1