简体   繁体   English

如何获取此linq查询以返回正确的值

[英]How to get this linq query to return correct values

var topTen = (from ep in db.VisitedCities
                            join c in db.Countries on ep.PersonWhoVisitedNationalityId equals c.CountryId
                            join e in db.Cities on ep.CityId equals e.CityId
                            join t in db.Countries on e.CountryId equals t.CountryId
                            where t.Name == "Portugal"
                            select ep.PersonWhoVisitedNationality).ToList();

The result of this returns a list with several items but all of them are null, what im i missing here? 这样的结果返回一个包含多个项目的列表,但是所有项目都为空,我在这里错过了什么?

I am expecting to get a list of nationalities (they are of type Country ) 我希望得到一份国籍列表(它们是Country类型的)

Thanks in advance. 提前致谢。

EDIT : 编辑:

Ok so the first question was related to this, what i want in the end is something like this (which works but only if I put the .ToList() in the middle :( ) 好的,所以第一个问题与此相关,我最终想要的是这样的东西(它可以工作,但.ToList()是我将.ToList()放在中间:()

var topTen = (from ep in db.VisitedCities
                              join e in db.Cities on ep.CityId equals e.CityId
                              join t in db.Countries on e.CountryId equals t.CountryId
                              where t.Name == "Portugal"
                              select ep)**.ToList()**
                              .GroupBy(x => x.PersonWhoVisitedNationality)
                              .Select(cp => new
                              {
                                  CountryName = cp.Key,
                                  NumberOfTravelers = cp.Count()
                              })
                              .OrderByDescending( x => x.NumberOfTravelers)
                              .Take(10)
                              .ToList();

Note that I am using the new entity framework 7 and I think for now the include extention does not work yet... So to summer up this query works fine but only if the .ToList() is in the middle :( 请注意,我正在使用新的实体框架7,并且我认为现在include扩展尚不起作用...因此,在夏季,此查询可以正常工作,但.ToList().ToList()位于中间:(

Ok so since EF7 is still in beta and the query was super slow i ended up creating an extension method for EF7 in order to do raw SQL query's and ended up like this : 好的,因为EF7仍处于测试阶段,并且查询速度非常慢,所以我最终为EF7创建了扩展方法,以便执行原始SQL查询,并最终如下所示:

var top10PT = db.Database.ExecuteSqlCommandExtensionMethod(@"
            select top 10 Nationality.Name, count(*) AS NumberOfTravelers FROM
            (
            select  [PersonWhoVisitedId]   FROM VisitedCity 
            INNER JOIN City ON VisitedCity.CityId = City.CityId 
            INNER JOIN Country ON City.CountryId = Country.CountryId
            WHERE Country.Alpha2Code = 'PT'
            ) AS Subquery
            INNER JOIN Person ON Person.PersonId = Subquery.PersonWhoVisitedId
            INNER JOIN Country as Nationality ON Person.NationalityId = Nationality.CountryId
            GROUP BY Nationality.Name
            ORDER BY NumberOfTravelers desc
            ").ToList();

Now it's really fast :D. 现在真的非常快:D。

Here's my extension method in case anyone is wondering : 如果有人想知道,这是我的扩展方法:

public static class Entity7Extensions
{
    public static IEnumerable<dynamic> ExecuteSqlCommandExtensionMethod(this DatabaseFacade database, string sql)
    {
        var connection = database.GetDbConnection();
        var command = connection.CreateCommand();
        command.CommandText = sql;

        try
        {
            connection.Open();

            var reader = command.ExecuteReader();

            while (reader.Read())
            {
                var expandoObject = new ExpandoObject() as IDictionary<string, object>;

                for (var i = 0; i < reader.FieldCount; i++)
                {
                    object propertyValue = reader.GetValue(i);

                    if (propertyValue is System.DBNull)
                        propertyValue = null;

                    expandoObject.Add(reader.GetName(i), propertyValue);
                }

                yield return expandoObject;
            }
        }
        finally
        {
            connection.Close();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM