Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
C# LINQ to实体无法识别自定义方法_C#_Linq_Linq To Entities - Fatal编程技术网

C# LINQ to实体无法识别自定义方法

C# LINQ to实体无法识别自定义方法,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,下面的代码产生错误: LINQ to Entities无法识别方法System.String GenerateSubscriptionButton(Int32)方法,此方法无法转换为存储表达式 如何在LINQ to实体中创建正确的自定义方法 var model = _serviceRepository.GetProducts().Select(p => new ProductModel { Id = p.Id, Name = p.Name, Credits = p.

下面的代码产生错误:

LINQ to Entities无法识别方法
System.String GenerateSubscriptionButton(Int32)
方法,此方法无法转换为存储表达式

如何在LINQ to实体中创建正确的自定义方法

var model = _serviceRepository.GetProducts().Select(p => new ProductModel
{
    Id = p.Id,
    Name = p.Name,
    Credits = p.Credits,
    Months = p.Months,
    Price = p.Price,
    PayPalButton = GenerateSubscriptionButton(p.Id)
});        

private string GenerateSubscriptionButton(int id)
{
    return new PaymentProcessor.PayPalProcessor().CreateSubscriptionButton(id);
}

你不能那样做。提供者应该如何将您的方法转换为SQL

记住:LINQtoEntities实际上并不执行查询的C代码。相反,它解释表达式并将其转换为SQL

在您的conrete案例中,解决方案可能如下所示:

var model = _serviceRepository.GetProducts()
                              .Select(p => new ProductModel 
                                           { 
                                               Id = p.Id, 
                                               Name = p.Name, 
                                               Credits = p.Credits, 
                                               Months = p.Months, 
                                               Price = p.Price
                                           })
                              .ToList()
                              .Select(x =>
                                      {
                                          x.PayPalButton = GenerateSubscriptionButton(x.Id);
                                          return x;
                                      }); 

调用
ToList
对数据库执行到目前为止的查询并返回结果。从那时起,查询实际上是一个LINQ to objects查询,其中代码不被解释,而是被执行。

您不能这样做。提供者应该如何将您的方法转换为SQL

记住:LINQtoEntities实际上并不执行查询的C代码。相反,它解释表达式并将其转换为SQL

在您的conrete案例中,解决方案可能如下所示:

var model = _serviceRepository.GetProducts()
                              .Select(p => new ProductModel 
                                           { 
                                               Id = p.Id, 
                                               Name = p.Name, 
                                               Credits = p.Credits, 
                                               Months = p.Months, 
                                               Price = p.Price
                                           })
                              .ToList()
                              .Select(x =>
                                      {
                                          x.PayPalButton = GenerateSubscriptionButton(x.Id);
                                          return x;
                                      }); 

调用
ToList
对数据库执行到目前为止的查询并返回结果。从那时起,查询实际上是一个LINQ to objects查询,其中代码不被解释,而是被执行。

您不能。问题是,您不能从SQL调用
GenerateSubscriptionButton

您需要检索实体,然后一旦它们进入内存,就可以调用
GenerateSubscriptionButton
。在将实体投影到模型上之前,可以通过添加对
AsEnumerable
的调用来实现这一点

var model = _serviceRepository.GetProducts()
    .AsEnumerable()
    .Select(p => new ProductModel
                     {
                         Id = p.Id,
                         Name = p.Name,
                         Credits = p.Credits,
                         Months = p.Months,
                         Price = p.Price,
                         PayPalButton = GenerateSubscriptionButton(p.Id)
                     });

你不能。问题是,您不能从SQL调用
GenerateSubscriptionButton

您需要检索实体,然后一旦它们进入内存,就可以调用
GenerateSubscriptionButton
。在将实体投影到模型上之前,可以通过添加对
AsEnumerable
的调用来实现这一点

var model = _serviceRepository.GetProducts()
    .AsEnumerable()
    .Select(p => new ProductModel
                     {
                         Id = p.Id,
                         Name = p.Name,
                         Credits = p.Credits,
                         Months = p.Months,
                         Price = p.Price,
                         PayPalButton = GenerateSubscriptionButton(p.Id)
                     });