Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc LINQ中的c调用方法_Asp.net Mvc_Entity Framework_Linq To Sql - Fatal编程技术网

Asp.net mvc LINQ中的c调用方法

Asp.net mvc LINQ中的c调用方法,asp.net-mvc,entity-framework,linq-to-sql,Asp.net Mvc,Entity Framework,Linq To Sql,我正在使用LINQ,我想通过传递参数调用一个方法,例如checkOpenClose,并让它返回true/false: 我得到以下错误: LINQ to实体无法识别方法“Boolean checkOpenClose” System.String,System.String,System.String,System.String,System.String, System.String,System.String,System.String,System.String,System.String,

我正在使用LINQ,我想通过传递参数调用一个方法,例如checkOpenClose,并让它返回true/false:

我得到以下错误:

LINQ to实体无法识别方法“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,System.String, System.String,System.String'方法,此方法不能为空 翻译成商店表达式


我做错了什么?

请记住,查询将被转换为sql,因此您的Linq提供程序无法将您的方法调用转换为有效的内容。 如果您确实需要调用该方法,那么我建议调用一个可数扩展方法,如下所示:

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

这将允许您使用Linq执行所需的投影到对象

您的布尔方法中有没有SQL对应的表达式…这已经充实了很多次,请参见


正如我看到的,您正在使用实体框架datacontext,它将linq语句转换为sql脚本,因此当您使用方法时,它无法将其转换为sql。 试试这个

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

我试过你的方法,它不承认帐户,这工作!非常感谢。当使用.AsEnumerable时,它执行_ctx.account中的DB from帐户,其中。。。选择account,然后选择select…..ToList从内存中获取值?试着去理解.AsEnumerable做什么以及什么时候应该使用它。如果我错了,请纠正我。在您的数据库中执行一个可数呼叫之前是什么情况。ToList和AsEnumerable之间的主要区别在于AsEnumerable保持不变。有关您的问题的更多信息,请参阅
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();