Skip to content

Instantly share code, notes, and snippets.

@Mart-Bogdan
Created January 9, 2017 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mart-Bogdan/4172d7c6eb56125f500445a5acc1d1db to your computer and use it in GitHub Desktop.
Save Mart-Bogdan/4172d7c6eb56125f500445a5acc1d1db to your computer and use it in GitHub Desktop.
Samples of using selects with entity framework
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