如果不过滤1:许多关系正常

时间:2017-03-30 01:26:23

标签: c# asp.net-mvc entity-framework linq linq-to-entities

我遇到了一个问题,我无法正确过滤使用a.ListOfEntities.Any(t => t.Id == id)的1-Many的linq关系;结果不是我想要过滤的是我的代码

详情实体

public class RequestCollection : Entity
{
    public int Id { get; set; }
    public DateTime JoinDate { get; set; }
    public int RequestId { get; set; }
    public string Reason { get; set; }
    public virtual  SubjectRequest Request { get; set; }
    public int StudentId { get; set; }
    public  virtual Student Student { get; set; }
}

家长实体

 public class SubjectRequest : Entity
{
    public int Id { get; set; }
    public int StudentId { get; set; }
    public int SubjectId { get; set; }
    public string Reason { get; set; }
    public string Year { get; set; }
    public string Semester { get; set; }
    public DateTime CreationDate { get; set; }
    public string Status { get; set; }
    public bool Open { get; set; }
    public virtual ICollection<RequestCollection> RequestCollections { get; set; }
    public virtual Student Initiator { get; set; }
    public virtual  Subject Subject { get; set; }
}

Fluent Mapping

public class RequestCollectionMap :EntityTypeConfiguration<RequestCollection>
{
    public RequestCollectionMap()
    {
        this.HasKey(t => t.Id);
        this.Property(t => t.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.HasRequired(t => t.Request)
            .WithMany(t => t.RequestCollections)
            .HasForeignKey(t => t.RequestId);

        this.HasRequired(t => t.Student)
            .WithMany(t => t.Requests)
            .HasForeignKey(t => t.StudentId);


    }
}

public class SubjectRequestMap :EntityTypeConfiguration<SubjectRequest>
{
    public SubjectRequestMap()
    {
        this.HasKey(t => t.Id);

        this.HasMany(t => t.RequestCollections)
            .WithRequired(t => t.Request)
            .HasForeignKey(t => t.RequestId);

        this.HasRequired(t => t.Initiator)
            .WithMany(t => t.SubjectRequests)
            .HasForeignKey(t => t.StudentId);

        this.HasRequired(t => t.Subject)
            .WithMany(t => t.Requests)
            .HasForeignKey(t => t.SubjectId);
    }
}

以下是过滤错误的代码,我的目的是根据他的年份,大学和课程筛选出适合学生的主题请求,但是消除了他目前加入的地方但过滤掉了错误。

return
            _repository.Query(a => a.Subject.SemesterYear == year && a.Subject.CourseId == courseId &&!a.RequestCollections.Any(a=>a.StudentId==studentId))
                .Include(a => a.Subject)
                .Include(a=> a.Subject.College)
                .Include(a => a.Initiator)
                .Include(a => a.RequestCollections)
                .Select(a => new RequestGridSource()
                {
                    Id = a.Id,
                    CreationDate = a.CreationDate,
                    Initiator = a.Initiator.FirstName + " " + a.Initiator.LastName,
                    Open = a.Open,
                    Reason = a.Reason,
                    Semester = a.Semester,
                    Status = a.Status,
                    Subject = a.Subject.Code,
                    Year = a.Year,
                    PhotoUrl = a.Subject.College.PhotoPath,
                    TotalCount = a.RequestCollections.Count
                }).AsQueryable();

发送数据库查询

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[StudentId] AS [StudentId], 
[Extent1].[SubjectId] AS [SubjectId], 
[Extent1].[Reason] AS [Reason], 
[Extent1].[Year] AS [Year], 
[Extent1].[Semester] AS [Semester], 
[Extent1].[CreationDate] AS [CreationDate], 
[Extent1].[Status] AS [Status], 
[Extent1].[Open] AS [Open]
FROM  [dbo].[SubjectRequests] AS [Extent1]
INNER JOIN [dbo].[Subjects] AS [Extent2] ON [Extent1].[SubjectId] = [Extent2].[Id]
WHERE ([Extent2].[SemesterYear] = 5) AND ([Extent2].[CourseId] = 2) AND ( EXISTS (SELECT 
    1 AS [C1]
    FROM [dbo].[RequestCollections] AS [Extent3]
    WHERE [Extent1].[Id] = [Extent3].[RequestId]
))

如果我将删除行 WHERE [Extent1]。[Id] = [Extent3]。[RequestId] 输出将是正确的

这是我在Request Collections DB中的记录

Id  JoinDate               RequestId    StudentId   Reason
62  2017-03-24 11:29:06.237   1            1         NULL
71  2017-03-28 12:51:07.060   4            4         NULL
72  2017-03-28 12:51:08.153   7            4         NULL
73  2017-03-28 12:51:08.853   5            4         NULL
87  2017-03-29 18:00:58.117   6            18        NULL
88  2017-03-29 18:01:20.797   9            18        NULL
101 2017-03-29 23:10:56.917   12           2         NULL

主题要求

Id  StudentId   SubjectId   Reason  Year    Semester    CreationDate    Status  Open
6   2   89  Reason  5   2   2017-03-24 16:42:30.447 CLOSED  0
8   2   90  Reason  5   2   2017-03-29 13:37:45.827 CLOSED  0
9   2   93  Reason  5   2   2017-03-29 14:54:13.810 CLOSED  0
10  2   94  Reason  5   1   2017-03-29 16:24:16.540 PENDING 0
11  17  106 Reason  5   2   2017-03-29 17:43:41.353 PENDING 0
12  2   105 Reason  5   2   2017-03-29 18:09:46.413 PENDING 0

以下是过滤但不那么干净的代码,因为我需要从数据库中获取单独的列表,然后通过requestId对其进行分组

 var req= _requestService.GetGridSources(student.Id,student.CourseId,student.Year);
        var collections = _collectionService.GetStudentRequests(student.Id);
        var result = req.Concat(collections)
                         .GroupBy(x => x.Id)
                         .Where(x => x.Count() == 1)
                         .Select(x => x.FirstOrDefault())
                         .ToList();
        return result;

第一个List获取基于学生站立的所有可能请求,第二个列表获取他加入的请求并按requestId对其进行分组。 它可能是EF中的某个错误或有人可以指出我的代码有什么问题..

0 个答案:

没有答案