Last active
November 28, 2021 16:23
-
-
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
This file contains 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 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