-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;
    }

それは、DropdDownList をバインドしてデータを表示するために使用できる Country オブジェクトのリストを返す必要があります。バインドするときは、 を使用して、オブジェクトから表示する特定の属性を選択します。そのため、後で別のデータ読み込み方法で使用できるように 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

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

国名のみが重要な場合は、以下を使用できます... 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;
    }
}

と が気になる場合は、次のようにしcountryIdます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 に答える
0

これが欲しいらしい

countries= (from c in context.Countries 
            select c.CountryName).Distinct()
于 2013-05-22T11:53:20.923 に答える
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 と Year を考慮しない場合に機能します。

于 2013-05-22T13:26:53.847 に答える