简体   繁体   English

C# - Linq:无法创建类型的常量值在此上下文中仅支持基本类型或枚举类型。

[英]C# - Linq : Unable to create a constant value of type Only primitive types or enumeration types are supported in this context.

I am using this linq query for login 我正在使用此linq查询进行登录

var login = context.Person_Login
                   .Where(c => c.Username == username && c.Password == password)
                   .DefaultIfEmpty(new Person_Login({Id = -1})
                   .First();

but in execution throw this exception: 但在执行中抛出此异常:

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll EntityFramework.SqlServer.dll中出现未处理的“System.NotSupportedException”类型异常

Additional information: Unable to create a constant value of type 'MyProject.MyModels.Person_Login'. 附加信息:无法创建“MyProject.MyModels.Person_Login”类型的常量值。 Only primitive types or enumeration types are supported in this context. 在此上下文中仅支持基元类型或枚举类型。

The exception message is quite descriptive. 异常消息非常具有描述性。

DefaultIfEmpty(new Person_Login({Id = -1})

is not supported in Linq to Entities. Linq to Entities不支持。

You can use the following instead 您可以使用以下代码

var login = context.Person_Login
    .FirstOrDefault(c => c.Username == username && c.Password == password)
    ?? new Person_Login {Id = -1};

Note that DefaultIfEmpty method is used mainly for performing LINQ left outer joins. 请注意, DefaultIfEmpty方法主要用于执行LINQ左外连接。

try 尝试

var login = context.Person_Login
                   .Where(c => c.Username == username &&
                   c.Password ==password)                       
                   .FirstOrDefault();
login = (login != null) ? login : (new Person_Login({Id = -1}));

Try 尝试

 var login = context.Person_Login
                   .Where(c => c.Username == username && c.Password == password)
                   .FirstOrDefault(new Person_Login({Id = -1});
var login = context.Person_Login
.Any(c => c.Username == username && c.Password == password) ? 
    context.Person_Login.First(c => c.Username == username && c.Password == password) :
     new Person_Login({Id = -1});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型。 在Linq C#中 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context. in Linq C# linq c# 无法创建“”类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - linq c# Unable to create a constant value of type ''. Only primitive types or enumeration types are supported in this context 无法创建类型“”的常量值。 在此上下文中仅支持原始类型或枚举类型。 ASP.NET MVC LINQ EF - Unable to create a constant value of type ''. Only primitive types or enumeration types are supported in this context. ASP.NET MVC LINQ EF LINQ,无法创建XXX类型的常量值。在此上下文中仅支持基元类型或枚举类型 - LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context LINQ查询错误:无法创建类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - LINQ Query error: Unable to create a constant value of type. Only primitive types or enumeration types are supported in this context Linq 到实体框架:无法创建“”类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - Linq to Entity Framework : unable to create a constant value of type ''. Only primitive types or enumeration types are supported in this context 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型两个db Linq查询 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context two db Linq query EntityFramework无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型 - EntityFramework Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context EF错误无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型 - EF error Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context 无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM