使用linq和相关实体查询实体框架7

时间:2015-06-13 20:16:46

标签: c# mysql linq entity-framework

我有这个查询女巫工作正常:

List<IGrouping<Country, VisitedCity>> tempQuery = null;
        using (var db = new MyDataContext())
        {
            tempQuery = db.VisitedCities.Include(c => c.PersonWhoVisited).Include(c => c.PersonWhoVisitedNationality).Include(c => c.City)
                .GroupBy(c => c.PersonWhoVisitedNationality)
                .ToList();
        }

        var dataInput = tempQuery
            .OrderBy(c => c.Key.Name)
            .Select(cp => new
            {
                CountryName = cp.Key.Name,
                VisitationsByCity = cp
                .GroupBy(x => x.City, x => x.CityId)
                .Select(c => new
                {
                    City = c.Key,
                    NumberOfVisits = c.Count()
                })
            }).ToList();

但问题是它正在将所有数据加载到我的应用程序中(我现在已经在我最大的表上已有30万行),并且它当然会逐渐变慢,因为它在ToList()方法调用中加载所有数据。

我将这个分为两个调用因为我无法弄清楚如何对数据库进行单次调用并仅返回dataInput,如果我将两个调用合并为一个i得到“对象引用未设置为对象的实例“。 exeption,这可能是因为没有包含一些引用,但我无法弄清楚我在查询中包含哪些表...

我也使用实体框架7仍然不支持延迟加载并且仍然缺少某些功能,但这应该是可能的吗? 在包含i尝试使用select语句

.Include(c => c.LadiesInWaiting.Select(b => b.Princess))

就像这里提到的那样:http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

但未被识别(因为它的新实体框架7?)

更新: 好的,如果我使用.AsEnumerable我可以在一个查询中完成所有操作,但是在下一次调用中加载数据似乎需要大约6秒钟,并且在这个时间点加载250 MB的内存...

var tempQuery = db.VisitedCities.Include(c => c.PersonWhoVisitedNationality).Include(c => c.City)
                .GroupBy(c => c.PersonWhoVisitedNationality)
                .AsEnumerable()
                .OrderBy(c => c.Key.Name)
                .Select(cp => new
                {
                    CountryName = cp.Key.Name,
                    VisitationsByCity = cp
                    .GroupBy(x => x.City, x => x.CityId)
                    .Select(c => new
                    {
                        City = c.Key,
                        NumberOfVisits = c.Count()
                    })
                });

            var allCities1 = tempQuery
            .SelectMany(x => x.VisitationsByCity.Select(c => c.City))
            .Distinct().OrderBy(city => city.Name).ToList();

1 个答案:

答案 0 :(得分:0)

好吧所以我管理(排序:P)将所有内容放在一个查询中,但我仍然需要调用.ToList()以便下一次数据检索工作,如果有人有更好的解决方案请让我知道。

var dataInput = db.VisitedCities
                .GroupBy(c => c.PersonWhoVisitedNationalityId)
                .Join(db.Countries, s => s.Key, c => c.CountryId, (s, c) => new { s, c })
                .OrderBy(c => c.c.Name)
                .Select(cp => new
                {
                    Country = cp.c,
                    VisitationsByCity = cp.s
                    .GroupBy(x => x.CityId)
                    .Join(db.Cities, s => s.Key, c => c.CityId, (s, c) => new { s, c })
                    .Select(c => new
                    {
                        City = c.c,
                        NumberOfVisits = c.s.Count()
                    })
                }).ToList();