Difference Between Select and SelectMany

后端 未结 17 1034
长情又很酷
长情又很酷 2020-11-22 05:21

I\'ve been searching the difference between Select and SelectMany but I haven\'t been able to find a suitable answer. I need to learn the differenc

17条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 05:44

    Without getting too technical - database with many Organizations, each with many Users:-

    var orgId = "123456789";
    
    var userList1 = db.Organizations
                       .Where(a => a.OrganizationId == orgId)
                       .SelectMany(a => a.Users)
                       .ToList();
    
    var userList2 = db.Users
                       .Where(a => a.OrganizationId == orgId)
                       .ToList();
    

    both return the same ApplicationUser list for the selected Organization.

    The first "projects" from Organization to Users, the second queries the Users table directly.

提交回复
热议问题