无法创建常量值类型。(Unable to create a constant value of type . Only primitive types or enumeration types are

编程入门 行业动态 更新时间:2024-10-26 21:31:46
无法创建常量值类型。(Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context)

我有以下方法进行通用检查,

public virtual bool CheckExist(T entity) { var context = new eTRdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }

但是它返回异常:

无法创建常量值类型。 在此上下文中仅支持原始类型或枚举类型。

请指教,

非常感谢,

I have the following method for a generic check exist,

public virtual bool CheckExist(T entity) { var context = new eTRdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }

However it returns the exception:

Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context.

Please advice,

Many Thanks,

最满意答案

尝试通过接受方法名称旁边的类型T来更改代码,如下所示:

public virtual bool CheckExist<T>(T entity) { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }

您可能还希望仅将类型限制为类,如下所示:

public virtual bool CheckExist<T>(T entity) where T : class { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }

Try changing the code by taking in the type T next to the method name, like this:

public virtual bool CheckExist<T>(T entity) { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }

You may also want to restrict the type to classes only, like this:

public virtual bool CheckExist<T>(T entity) where T : class { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }

更多推荐

本文发布于:2023-08-01 03:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:量值   类型   create   constant   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!