Created
January 9, 2017 20:30
-
-
Save Mart-Bogdan/4172d7c6eb56125f500445a5acc1d1db to your computer and use it in GitHub Desktop.
Samples of using selects with entity framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ICollection<Tuple<Site,ICollection<String>>>> GetSitesByUserIdAsync(int userId) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
Action<string> action = s => sb.Append(s); | |
_context.Database.Log += action; | |
var res= await _context.UserSiteAccesses | |
.Where(p => p.UserId == userId && p.Accsess == "read") | |
.Select(p => new RwTuple<Site,ICollection<String>> | |
{ | |
Item1 = p.Site, | |
Item2 = _context.UserSiteAccesses | |
.Where(a | |
=> a.UserId == userId | |
&& a.SiteId==p.SiteId | |
) | |
.Select(a=>a.Accsess).ToList() | |
} | |
) | |
.ToListAsync(); | |
_context.Database.Log -= action; | |
var sql = sb.ToString(); | |
return res.Select(x => x.ToTuple()).ToList(); | |
} | |
public async Task<int> GetOwnerId(int siteId) | |
{ | |
var selectionResult = await base._context.Sites | |
.Where ( p => p.Id == siteId ) | |
.Select (s=>s.OwnerId) | |
.FirstOrDefaultAsync(); | |
return selectionResult; | |
} | |
public async Task<List<Site>> GetSitesByUserId(int userId) | |
{ | |
var sites = await base._context.UserSiteAccesses | |
.Where(p => p.UserId == userId) | |
.GroupBy(p=>p.Site) | |
.Select(p => p.Key) | |
.ToListAsync(); | |
return sites; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment