簡體   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();

這樣的結果返回一個包含多個項目的列表,但是所有項目都為空,我在這里錯過了什么?

我希望得到一份國籍列表(它們是Country類型的)

提前致謝。

編輯:

好的,所以第一個問題與此相關,我最終想要的是這樣的東西(它可以工作,但.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();

請注意,我正在使用新的實體框架7,並且我認為現在include擴展尚不起作用...因此,在夏季,此查詢可以正常工作,但.ToList().ToList()位於中間:(

好的,因為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();

現在真的非常快:D。

如果有人想知道,這是我的擴展方法:

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