LINQ,无法创建类型XXX的恒定值。只有原始类型或枚举类型在这种情况下,支持 [英] LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context

查看:444
本文介绍了LINQ,无法创建类型XXX的恒定值。只有原始类型或枚举类型在这种情况下,支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有讲师,他们有课程表,他们可以教,当我删除我当然想删除连接讲师。这里的code:

In my application I have Lecturers and they have list of Courses they can teach and when I'm deleting a course I want to remove connection to lecturers. Here's the code:

public void RemoveCourse(int courseId)
{
    using (var db = new AcademicTimetableDbContext())
    {
        var courseFromDb = db.Courses.Find(courseId);

        var toRemove = db.Lecturers
                        .Where(l => l.Courses.Contains(courseFromDb)).ToList();

        foreach (var lecturer in toRemove)
        {
            lecturer.Courses.Remove(courseFromDb);
        }

        db.SaveChanges();
    }
}

,但它不工作。我得到

but it doesn't work. I get

NotSupportedException异常:无法创建类型的恒定值课程。只有基本类型或枚举类型在这方面的支持。

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

我在做什么错了?

推荐答案

您不能使用包含与非基本值。做

You can't use Contains with non-primitive values. Do

Where(l => l.Courses.Select(c => c.CourseId).Contains(courseId)

(或者你使用id字段)。

(or the Id field you use).

这篇关于LINQ,无法创建类型XXX的恒定值。只有原始类型或枚举类型在这种情况下,支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆