尝试以通用方式调用方法时出现错误

尝试以通用方式调用方法时出现错误

本文介绍了尝试以通用方式调用方法时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试通过将参数传递给如下所示的方法来使用where类过滤表

Hi all I am trying to filter the table using where class by passing parameter to the method like as below,

    private IQueryable<SpaceFunctionType> Get<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class, ICategorySpaceFunction
    {
        return _dbContext.Set<TEntity>().Where(predicate)
                              .Select(c => new SpaceFunctionType
                              {
                                  Category = c.Category,
                                  SpaceFunction = c.SpaceFunction
                              });
    }

   public IQueryable<SpaceFunctionType> GetSpaceFunctionType(string environmentSource)
   {
        return Get<LibraryEnvironment>(x => x.EnvironmentSource.Name == environmentSource).AsQueryable();
   }

这些是界面详细信息,

public interface ICategorySpaceFunction
{
    public string Category { get; set; }
    public string SpaceFunction { get; set; }
}

这将是类名

public class SpaceFunctionType : ICategorySpaceFunction
{
    public string Category { get; set; }
    public string SpaceFunction { get; set; }
}

但是在这里出现错误Get<LibraryEnvironment>如下所示

but getting an error here Get<LibraryEnvironment> like as below

我不确定上述方法在哪里出了问题,请问有人可以提出任何对我非常感激的想法,

I am not sure where i am doing wrong with the above method, Could any one please suggest any idea on this that would be very grateful to me,

非常感谢

推荐答案

错误正在发生.但是那里使用的语言有点通用.这意味着LibraryEnvironment没有实现ICategorySpaceFunction. TEntityICategorySpaceFunction的一种,您在调用时发送LibraryEnvironment,因此LibraryEnvironment应该与TEntity类似,这是ICategorySpaceFunction的实现,这是一个规则.更多详细信息- https://docs.microsoft.com/zh-CN/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters

The error is what it is happening. But the language used there is a bit generic. What it means is LibraryEnvironment does not implement ICategorySpaceFunction. TEntity is a type of ICategorySpaceFunction and you are sending LibraryEnvironment when calling, so it is a rule that LibraryEnvironment should be similar to TEntity, which is an implementation of ICategorySpaceFunction. more details - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters

这篇关于尝试以通用方式调用方法时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 20:13