使用linq获取三个记录的重复ID

时间:2019-12-10 23:03:05

标签: c# .net entity-framework linq c#-4.0

您好,我拥有以下数据模型

public class Request
{
    public Guid RequestId { get; set; }
    public string Description { get; set; }
    public Guid DataId { get; set; }
    public DateTime CreatedAt { get; set; }
    public Guid MasterSectionId { get; set; }
    public Guid RequestStatusId { get; set; }
    public Guid RequestStageId { get; set; }
    public Guid RequestTypeId { get; set; }
    public  MasterSection MasterSection { get; set; }
    public  RequestStatus Status { get; set; }
    public  RequestStage Stage { get; set; }
    public  RequestType RequestType { get; set; }
}

public class RequestStage
{

    public Guid RequestStageId { get; set; }
    public string Name { get; set; }
    public ICollection<Request> Requests { get; set; }
}
public class MasterSection
{
    public Guid MasterSectionId { get; set; }
    public string Name { get; set; }
    public ICollection<LocalCode> LocalCodes { get; set; }
    public ICollection<Request> Requests { get; set; }
}
public class RequestStatus
{
    public Guid  RequestStatusId { get; set; }
    public string Name { get; set; }
    public ICollection<Request> Requests { get; set; }
}
public class LocalCode 
{
    public Guid? Id { get; set; }
    public string Name { get; set; }
    public Guid MasterSectionId { get; set; }
    public MasterSection MasterSection { get; set; }
}

public class RequestType
{
    public Guid RequestTypeId { get; set; }
    [Column(TypeName = "varchar")]
    public RequestActionEnum Name { get; set; }
    public ICollection<Request> Requests { get; set; }
}

在这些表RequestType,RequestStage,requestStatus中我已经有一种示例数据

RequestStage
  id    name 
  1     RequestSubmitted    
requestStatus 
  id    Name
  1      Update

当我要将新的本地代码条目插入localcode表中时,我将在request表中插入一行以及相应的值。

我正在尝试检索具有Request Submitted之类的requesStage的请求的结果 查询:

   var results = _dbContext.Requests.Include(r => r.Stage).Include(r => r.MasterSection)
                 .Where(r => r.Stage.Name == "RequestSubmitted")
                 .Join(_dbContext.LocalCodes.Include(l => l.MasterSection), rqst => rqst.MasterSectionId, lc => lc.MasterSectionId,
                  (rt, lc) => new { r = rt, rt = rt.RequestType, rs = rt.Stage, lc = lc, ms = lc.MasterSection }).AsEnumerable().
                  Select(secResponse => new SectionResponse
                  {
                     Id = secResponse.r.RequestId,
                     Section = secResponse.lc.Name,
                     Description = secResponse.lc.Description,
                     CreatedBy = "",
                     Type = secResponse.rt.Name.ToString(),
                     Status = secResponse.rs.Name,
                     Age = (DateTime.Now.Date - secResponse.r.CreatedAt.Date).TotalDays.ToString() + "Days"

                  }).GroupBy(i => (i.Id)).Where(x => x.Count() > 1).FirstOrDefault().ToList();

结果

  {
    "id": "3a689a70-eb43-4a99-9b6b-309920936900",
    "section": "pipingCode",
    "type": "New",
    "createdBy": "",
    "description": "pipingDesc",
    "status": "Request Submitted",
    "age": "1Days"
  },
  {
    "id": "3a689a70-eb43-4a99-9b6b-309920936900",
    "section": "Mechanical Code",
    "type": "New",
    "createdBy": "",
    "description": "mechDescription",
    "status": "Request Submitted",
    "age": "1Days"
  },
  {
    "id": "3a689a70-eb43-4a99-9b6b-309920936900",
    "section": "TestLocalCode1",
    "type": "New",
    "createdBy": "",
    "description": "testDescription",
    "status": "Request Submitted",
    "age": "1Days"
  }

结果应该是具有不同ID的三行,但是上面的查询获取了重复的ID,我不确定我在哪里做错了..

另一个问题是在评论中提到了什么。

there i am getting 9 rows with duplicate and here i am getting only ID's duplicate

任何人都可以提出任何对此表示感谢的想法吗?

非常感谢

0 个答案:

没有答案