-2

我有一个数据库表 Country ,其示例数据如下:

CountryId        CountryName         Year
1                 UK                 2001
2                 UK                 2003
3                 UK                 2004
4                 USA                2001
5                 USA                2005

我的 DataAccessLevel 上有一个 GetAllCountries() 方法:

public static IEnumerable<Country> GetAllCountries()
    {
        List<Country> countries;
        using (var context = new ReportEntities())
        {
            countries= (from c in context.Countries 
                       select c).ToList();
        }
           return countries;
    }

这应该返回一个 Country 对象列表,然后我可以使用它来绑定 DropdDownList 以显示数据。当我绑定时,我使用 来选择要从对象中显示的特定属性。所以我需要 List 以便以后可以在不同的数据加载方法中使用它。例如在 LoadCountriesToDdlList() 中:

{
       var countries= _transactionService.GetAllCountries();
        var distinctcountries = countries.GroupBy(c=> c.CountryName); 
        _UIDDListCountries.DataSource = distinctcountries ;
}

列表的预期结果: CountryName
UK
USA

我尝试了很多不同的方法来编辑查询,但每次都失败了。有任何想法吗?

尝试:GrouppBy,OrderedBy,Distinct(),选择新对象,但没有运气。问题似乎是我试图返回一个对象列表。

4

4 回答 4

0

听起来你想要这个

countries= (from c in context.Countries 
            select c.CountryName).Distinct()
于 2013-05-22T11:53:20.923 回答
0

GroupBy()应该给你你所追求的:

var grouped = context.Countries.GroupBy(c => c.CountryName);

    foreach (var country in grouped)
    {
        var distinctCountryName = country.Key;  //Access field used to group
        var firstMatchingCountry = country.First();
        var matchingCountriesInAList = country.ToList();
    }
于 2013-05-22T11:55:17.860 回答
0

如果您只希望下拉列表具有上表中不同的国家/地区名称,您可以尝试以下操作:

{
       var countries= _transactionService.GetAllCountries();
        var distinctcountries = countries.GroupBy(c=> c.CountryName); 
        _UIDDListCountries.DataSource = distinctcountries.Select(g => g.First());
}

上面的代码首先根据 CountryName 对所有国家对象进行分组,然后我们只将每个分组结果的第一个对象分配给下拉数据源。

如果您想自定义您的文本字段值,您可以创建一个匿名类型并使用它。下面的代码:

{
       var countries= _transactionService.GetAllCountries();
       var distinctcountries = countries.GroupBy(c=> c.CountryName); 
       _UIDDListCountries.DataSource = distinctcountries.Select(g => new { CountryID = g.First().CountryID, CountryName = g.First().CountryName ,Text = String.Concat(g.First().CountryName, "--", g.First().Year) }) ;
       _UIDDListCountries.DataTextField = "Text";
       _UIDDListCountries.DataValueField = "CountryName";
}

注意:当您只关心在下拉列表中显示不同的 CountryNames 值而不考虑 CountryID 和年份时,此方法有效

于 2013-05-22T13:26:53.847 回答
0

如果只有国家/地区名称很重要,您可以使用以下内容...如 countryId 和 year 不重要。

//Note, you'll probably want to change this function name because it's
//not actually getting all countries anymore
public static IEnumerable<Country> GetAllCountries()
{
    using (var context = new ReportEntities())
    {
        //Note, this LINQ query can also return an IQueryable.  This is useful
        //if you're querying a database because you'll be doing more logic in SQL
        //and transferring less data from your database to memory on your C# machine
        IEnumerable<Country> countries = 
                 from c in context.Countries
                 group c by c.CountryName into countriesGroupedByName
                 select countriesGroupedByName.First();
        return countries;
    }
}

如果您关心countryIdand ,请执行以下操作countryName

IEnumerable<Country> countries = 
                    from c in context.Countries
                    group c by c.CountryName into countriesGroupedByName
                    select countriesGroupedByName.OrderBy(c => c.CountryId).First();
于 2013-05-22T12:22:14.120 回答