Skip to content

Instantly share code, notes, and snippets.

@nmchenry01
Last active November 28, 2021 16:23
Show Gist options
  • Save nmchenry01/4d729c63d91f679927afbaaf504ccc46 to your computer and use it in GitHub Desktop.
Save nmchenry01/4d729c63d91f679927afbaaf504ccc46 to your computer and use it in GitHub Desktop.
An example of using LINQ in a C# repository to filter data by tenant ID
public class ProductRepository : IProductRepository
{
private readonly DatabaseContext _context;
public ProductRepository(DatabaseContext context)
{
_context = context;
}
public async Task<ProductModel> Get(int tenantId)
{
/*
LINQ query that compiles to something like the below in SQL:
SELECT * FROM product as p
WHERE p.tenant_id = {tenant_id};
*/
return await _context.Products
.Where(p => p.TenantId == tenantId)
.ToListAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment