How to map LINQ To SQL to enable eager loading, return EntitySet or ICollection?
这与我这里的问题有关(但相当独立):为什么选择没有外键和LINQ的N+1?
我试过用
我正在手动编写LinqToSQL映射,并首先遵循以下教程:http://www.codeproject.com/articles/43025/a-linq-tutorial-mapping-tables-to-objects
现在我找到了这个教程:http://msdn.microsoft.com/en-us/library/bb386950.aspx
我至少能发现一个主要的区别。第一个教程建议返回
在这一点上,我只想知道我应该用哪种返回类型来设计好的样式?我能有一个好的设计,在特定的情况下仍然能够强制加载吗?
经过多次反复试验,终于找到了解决办法。可以退回
一个再次出现的问题是返回分页结果,并对页面外的所有项目进行计数。这可以通过创建
有关从此处的存储库返回内容的详细信息:
http://www.codetunnel.com/blog/post/103/should-you-return-iqueryablet-from-your-stores(来自您的存储库)
http://www.shawnmclean.com/blog/2011/06/iqueryable-vs-ienumerable-in-the-repository-pattern/
用于业务逻辑或DAL返回类型的IEnumerable与iQueryable
list、ilist、ienumerable、iqueryable、icocollection,哪种返回类型最灵活?
更重要的是,
更多信息在这里:
https://stackoverflow.com/a/7941017/1312533
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementation-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
这就是我在我的存储库中最终得到的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public class SqlLocalizedCategoriesRepository : ILocalizedCategoriesRepository { private string connectionString; private HttpContextBase httpContext; public SqlLocalizedCategoriesRepository(string connectionString, HttpContextBase httpContext) // Injected with Inversion of Control { this.connectionString = connectionString; this.httpContext = httpContext; } public CollectionPage<Product> GetProductsByLocalizedCategory(string category, int countryId, int page, int pageSize) { // Setup a DataContext using (var context = new DataContext(connectionString)) // Because DataContext implements IDisposable it should be disposed of { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Product>(p => p.ProductSubs); // In this case I want all ProductSubs for the Products, so I eager load them with LoadWith. There's also AssociateWith which can filter what is eager loaded. context.LoadOptions = dlo; context.Log = (StringWriter)httpContext.Items["linqToSqlLog"]; // For logging queries, a must so you can see what LINQ to SQL generates // Query the DataContext var cat = (from lc in context.GetTable<LocalizedCategory>() where lc.CountryID == countryId && lc.Name == category select lc.Category).First(); // Gets the category into memory. Might be some way to not get it into memory by combining with the next query, but in my case my next step is that I'm also going to need the Category anyway so it's not worth doing because I'm going to restructure this code to take a categoryId parameter instead of the category parameter. var products = (from p in context.GetTable<Product>() where p.ProductCategories.Any(pm => pm.Category.CategoryID == cat.CategoryID) select p); // Generates a single query to get the the relevant products, which with DataLoadOptions loads related ProductSubs. It's important that this is just a query and not loaded into memory since we're going to split it into pages. // Return the results var pageOfItems = new CollectionPage<Product> { Items = products.Skip(pageSize * (page - 1)).Take(pageSize).ToList(), // Gets the page of products into memory TotalItems = products.Count(), // Get to total count of items belonging to the Category CurrentPage = page }; return pageOfItems; } } } |