gpt4 book ai didi

c# - LINQ TO ENTITY 无法与枚举类型进行比较

转载 作者:太空狗 更新时间:2023-10-30 01:29:40 27 4
gpt4 key购买 nike

下面是枚举叶子

public enum Leaves
{
Annual = 0,
Medical = 1,
Hospitalization = 2,
Unpaid = 3
}

下面是linq查询

public ActionResult ApproveLeave(int? id)
{
if (id == null)
return View();

LeaveApplication leaveApplication = db.LeaveApplication.Find(id);

if (leaveApplication == null)
return HttpNotFound();

leaveApplication.Status = "Approved";

if (ModelState.IsValid)
{
db.Entry(leaveApplication).State = EntityState.Modified;

Leaves leavesType = (Leaves)Enum.Parse(typeof(Leaves), leaveApplication.LeaveType.ToString());

var lb = (from t in db.LeaveBalance
select t)
.Where(t => t.LeaveType == leavesType && t.Profileid.Equals(id))
.FirstOrDefault();

lb.Taken++;
lb.Balance--;
db.SaveChanges();

return RedirectToAction("Index");
}

return View();
}

我什至尝试过使用 Leaves.Annual,但它不起作用。LINQ 查询抛出以下异常:

System.NotSupportedException HResult=0x80131515 Message=Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

最佳答案

Equals 在 Linq2ToEntity 中不受支持,您应该改用双等号:

var lb = (from t in db.LeaveBalance select t)
.Where(t => t.LeaveType == leavesType && t.Profileid == id)
.FirstOrDefault();

假设您的 Profileid 是一个整数,使用 == 应该可以在不改变逻辑或遇到大小写问题的情况下使其正常工作。

关于c# - LINQ TO ENTITY 无法与枚举类型进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50551107/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com