Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How to join two linq queries to find some common data between them

How to join two linq queries to find some common data between them

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqtutorial
3 Posts 3 Posters 27 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ElenaRez
    wrote on last edited by
    #1

    I have two linq queries like the following and their output results are correct:

    var mytotal = _context.Apiapplicant.Where(c => !c.ApiRequestDate.Equals("") && c.IsDeleted.Equals(false)).GroupBy(o => new
    {
    Month = o.ApiRequestDate.Substring(5, 2),
    Year = o.ApiRequestDate.Substring(0, 4)
    }).Select(g => new
    {
    Month = g.Key.Month,
    Year = g.Key.Year,
    Total = g.Count()
    }).OrderByDescending(a => a.Year).ThenByDescending(a => a.Month).ToList();

    The above query gives the total requests that was registerd in each month

    var numerator = from t1 in _context.Apiapplicant
    join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
    join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
    //join t4 in mytotal on new { t2.Date.Substring(0,4), t2.Date.Substring(2,5) } equals new { t4.Year, t4.Month }
    // join t4 in mytotal on t2.Date.Substring(2, 5) equals t4.Month

                            where t1.IsDeleted == false && t3.Name == "granted" && t2.Date != null && t1.ApiRequestNo != null
    
                            group t1
                            by new
                            {
                                lastReq = t2.LastReqStatus,
                                Year = t2.Date.Substring(0, 4),
                                Month = t2.Date.Substring(5, 2)
                            } into g
    
                            select new
                            {
                                Year = g.Key.Year,
                                Month = g.Key.Month,
                                lastReq = g.Key.lastReq,
                                GrantedCount = g.Count()
                            };
    
    
            var GrantedReqStatus = numerator.OrderByDescending(x => x.Year).ThenByDescending(a => a.Month).ToList();
    

    The above query gives total requests for each month that has "granted" status. Now I want to find for all the requests that are registered in each month, how many of them has "granted" status. I mean finding how many records in mytotal are in numerator for each month. To obtain it, I've made a query like the following which should join both GrantedReqStatus and mytotal queries:

    var LastGrantedStatus = (from t1 in mytotal
    from t2 in Gra

    L M 2 Replies Last reply
    0
    • E ElenaRez

      I have two linq queries like the following and their output results are correct:

      var mytotal = _context.Apiapplicant.Where(c => !c.ApiRequestDate.Equals("") && c.IsDeleted.Equals(false)).GroupBy(o => new
      {
      Month = o.ApiRequestDate.Substring(5, 2),
      Year = o.ApiRequestDate.Substring(0, 4)
      }).Select(g => new
      {
      Month = g.Key.Month,
      Year = g.Key.Year,
      Total = g.Count()
      }).OrderByDescending(a => a.Year).ThenByDescending(a => a.Month).ToList();

      The above query gives the total requests that was registerd in each month

      var numerator = from t1 in _context.Apiapplicant
      join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
      join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
      //join t4 in mytotal on new { t2.Date.Substring(0,4), t2.Date.Substring(2,5) } equals new { t4.Year, t4.Month }
      // join t4 in mytotal on t2.Date.Substring(2, 5) equals t4.Month

                              where t1.IsDeleted == false && t3.Name == "granted" && t2.Date != null && t1.ApiRequestNo != null
      
                              group t1
                              by new
                              {
                                  lastReq = t2.LastReqStatus,
                                  Year = t2.Date.Substring(0, 4),
                                  Month = t2.Date.Substring(5, 2)
                              } into g
      
                              select new
                              {
                                  Year = g.Key.Year,
                                  Month = g.Key.Month,
                                  lastReq = g.Key.lastReq,
                                  GrantedCount = g.Count()
                              };
      
      
              var GrantedReqStatus = numerator.OrderByDescending(x => x.Year).ThenByDescending(a => a.Month).ToList();
      

      The above query gives total requests for each month that has "granted" status. Now I want to find for all the requests that are registered in each month, how many of them has "granted" status. I mean finding how many records in mytotal are in numerator for each month. To obtain it, I've made a query like the following which should join both GrantedReqStatus and mytotal queries:

      var LastGrantedStatus = (from t1 in mytotal
      from t2 in Gra

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You already posted this question in the .NET forum. Please do not crosspost.

      1 Reply Last reply
      0
      • E ElenaRez

        I have two linq queries like the following and their output results are correct:

        var mytotal = _context.Apiapplicant.Where(c => !c.ApiRequestDate.Equals("") && c.IsDeleted.Equals(false)).GroupBy(o => new
        {
        Month = o.ApiRequestDate.Substring(5, 2),
        Year = o.ApiRequestDate.Substring(0, 4)
        }).Select(g => new
        {
        Month = g.Key.Month,
        Year = g.Key.Year,
        Total = g.Count()
        }).OrderByDescending(a => a.Year).ThenByDescending(a => a.Month).ToList();

        The above query gives the total requests that was registerd in each month

        var numerator = from t1 in _context.Apiapplicant
        join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
        join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
        //join t4 in mytotal on new { t2.Date.Substring(0,4), t2.Date.Substring(2,5) } equals new { t4.Year, t4.Month }
        // join t4 in mytotal on t2.Date.Substring(2, 5) equals t4.Month

                                where t1.IsDeleted == false && t3.Name == "granted" && t2.Date != null && t1.ApiRequestNo != null
        
                                group t1
                                by new
                                {
                                    lastReq = t2.LastReqStatus,
                                    Year = t2.Date.Substring(0, 4),
                                    Month = t2.Date.Substring(5, 2)
                                } into g
        
                                select new
                                {
                                    Year = g.Key.Year,
                                    Month = g.Key.Month,
                                    lastReq = g.Key.lastReq,
                                    GrantedCount = g.Count()
                                };
        
        
                var GrantedReqStatus = numerator.OrderByDescending(x => x.Year).ThenByDescending(a => a.Month).ToList();
        

        The above query gives total requests for each month that has "granted" status. Now I want to find for all the requests that are registered in each month, how many of them has "granted" status. I mean finding how many records in mytotal are in numerator for each month. To obtain it, I've made a query like the following which should join both GrantedReqStatus and mytotal queries:

        var LastGrantedStatus = (from t1 in mytotal
        from t2 in Gra

        M Offline
        M Offline
        Maciej Los
        wrote on last edited by
        #3

        I think you're trying to resolve your issue by complicating it too much... ;) Note, that Count can accept condition to get interesting data (which works like Where). See: [Enumerable.Count Method (System.Linq) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.count?view=netcore-3.1#System\_Linq\_Enumerable\_Count\_\_1\_System\_Collections\_Generic\_IEnumerable\_\_\_0\_\_) So...

        var finalTotal = from t1 in _context.Apiapplicant
        join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
        join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
        where t1.IsDeleted == false && t2.Date != null && t1.ApiRequestNo != null
        group t1
        by new
        {
        lastReq = t2.LastReqStatus,
        Year = t2.Date.Substring(0, 4),
        Month = t2.Date.Substring(5, 2)
        } into g
        select new
        {
        Year = g.Key.Year,
        Month = g.Key.Month,
        lastReq = g.Key.lastReq,
        Granted = g.Count(x=>x.t2.Name == "granted"),
        Other = g.Count(x=>x.t2.Name != "granted"),
        TotalCount = g.Count()
        };

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups