getting duplicate rows for each row using linq query

后端 未结 1 460
孤独总比滥情好
孤独总比滥情好 2021-01-26 05:33

I have db schema having tables like this below, i am trying to get the results using left join or normal join on these table using the below query. Somehow getting duplicate ro

相关标签:
1条回答
  • 2021-01-26 06:25

    I tried using navigational properties and based on the description came up with below domain models. Please try this: (code is not tested though)

    // Junction Table
    public class Requests
    {
       [Key]
       public int Id {get; set;}
       public DateTime createdAt {get; set;}
    
       [ForeginKey("RequestType")]
       public int RequestTypeId {get; set;}
       public virtual RequestType RequestType {get; set;}
    
       [ForeginKey("MasterSections")]
       public int MasterSectionID {get; set;}
       public virtual MasterSections MasterSections {get; set;}
    
       public int RequestStatusID {get; set;}
    
       [ForeginKey("RequestStage")]
       public int RequestStageID {get; set;}
       public virtual RequestStage RequestStage {get; set;}
    }
    
    public class RequestStage
    {
       [Key]
       public int RequestStageID {get; set;}
       public string name {get; set;}
    }
    
    public class MasterSections
    {
       public int MasterSectionId {get; set;}
       public string name {get; set;}
    }
    
    public class LocalCodes
    {
       [Key]
       public int Id {get; set;}
    
       [ForeginKey("MasterSections")]
       public int MasterSectionId {get; set;}
       public virtual MasterSections MasterSections {get; set;}
       public string Description {get; set;}
       public string name {get; set;}
    }
    

    If you want to use IQueryable then you can not calculate Age at the time of projection using .NET framework's DATE functions. You will need to use EF DATE related DBFunctions.

    db.Requests.Include(r => r.RequestStage).Include(r => r.MasterSections)
      .Where(r => r.RequestStage.name == "RequestSubmitted")
      .Join(_dbContext.LocalCodes.Include(l => l.MasterSections), rqst => rqst.MasterSectionID, lc => lc.MasterSectionId,
       (rt, lc) => new SectionResponse
                      {
                            Section = lc.Name,
                            Description = lc.Description,
                            CreatedBy = "",
                            Type = rt.Name.ToString(),
                            Status = rt.rs.Name,
                            /* Age property can not be done from within Queryable as .Date is not available in EF. If you want this, convert the query to enumerable and then project.*/
                            // Age = (DateTime.Now.Date - re.CreatedAt.Date).TotalDays.ToString() + "Days"
                       }).ToList();
    

    By converting to Enumerable, you can calculate Age at the time projection like below:

    db.Requests.Include(r => r.RequestStage).Include(r => r.MasterSections)
      .Where(r => r.RequestStage.name == "RequestSubmitted")
      .Join(_dbContext.LocalCodes.Include(l => l.MasterSections), rqst => rqst.MasterSectionID, lc => lc.MasterSectionId,
       (rt, lc) => new {rt = rt, rs = rt.RequestStage, lc = lc, ms = lc.MasterSections}).AsEnumerable().
      Select(anonType =>  new SectionResponse
                      {
                            Section = anonType.lc.Name,
                            Description = anonType.lc.Description,
                            CreatedBy = "",
                            Type = anonType.rt.Name.ToString(),
                            Status = anonType.rs.Name,
                            Age = (DateTime.Now.Date - anonType.rt.CreatedAt.Date).TotalDays.ToString() + "Days"
                       }).ToList();
    

    Based on update to Request model, it can be achieved using below query:

    db.Requests.Include(r => r.RequestStage).Include(r => r.RequestType).Include(r => r.LocalCodes.MasterSections)
      .Where(r => r.RequestStage.name == "RequestSubmitted")
      .Select(r => new {r = r, rt = r.RequestType, rs = rt.RequestStage, lc = r.lc, ms = r.lc.MasterSections}).AsEnumerable().
      Select(anonType =>  new SectionResponse
                      {
                            Section = anonType.lc.Name,
                            Description = anonType.lc.Description,
                            CreatedBy = "",
                            Type = anonType.rt.Name.ToString(),
                            Status = anonType.rs.Name,
                            Age = $"{DateTime.Now.Date.Subtract(anonType.r.CreatedAt.Date).TotalDays} Days"
                       }).ToList();  
    
    0 讨论(0)
提交回复
热议问题