0

ビューに渡すユーザー プロファイルのリストを並べ替えるのに問題があります。特定の役割のすべてのユーザーのリストを表示し、それらを familyName 属性で並べ替えたいと考えています。

OrderBy を使用してみましたが、効果がありません。

コントローラー内のコード

public ActionResult Index()
    {
        //get all patients
        var patients = Roles.GetUsersInRole("user").ToList();
        //set up list of patient profiles
        List<UserProfile> pprofiles = new List<UserProfile>();
        foreach (var i in patients) {
            pprofiles.Add(ZodiacPRO.Models.UserProfile.GetUserProfile(i));
        }
        pprofiles.OrderBy(x => x.familyName);   //<-this has no effect the list produced is
                                                // exactly the same it was without this line
        return View(pprofiles);
    }

そして、ビュー

   <ul id= "patientList">

        @foreach (var m in Model)
            {
                <li>
                <ul class="patient">
                 <li class="ptitle">@m.title</li>
                 <li class="pname"> @Html.ActionLink(@m.givenName + " " + @m.familyName, "View", "Account", new { @username = @m.UserName.ToString() }, new { id = "try" })</li>
                 <li class="pprofile">@Ajax.ActionLink("Profile", "PatientSummary", new { @username = @m.UserName }, new AjaxOptions { UpdateTargetId = "pContent"},new{ @class = "profpic" })</li>
                </ul>
                </li>         
            }
    </ul>

これを複数の場所で再利用する必要があり、多数のユーザーがいる可能性があるため、何らかの方法で注文しないとひどいことになります. これについてどうすればよいですか?

4

3 に答える 3

2

OrderBy はpprofiles要素の順序を変更するのではなく、要素が順序付けられた新しいコレクションを返します。これを試すことができます:

pprofiles = pprofiles.OrderBy(x => x.familyName);

または、 List(T).Sortを使用できます

于 2012-07-19T15:18:53.437 に答える
2

pprofiles.OrderBy(x => x.familyName);IEnumerable<T>は、呼び出された場所で配列をソートするのではなく、を返します。

次のようにコードを変更できます。

public ActionResult Index()
{
    //get all patients
    var patients = Roles.GetUsersInRole("user").ToList();
    //set up list of patient profiles

    List<UserProfile> pprofiles = new List<UserProfile>();
    foreach (var i in patients) {
        pprofiles.Add(ZodiacPRO.Models.UserProfile.GetUserProfile(i));
    }       
    var ordered = pprofiles .OrderBy(x => x.familyName);   

    return View(ordered );
}

またはよりLinqスタイルの方法で:

var orderedPatients = Roles.GetUsersInRole("user")
                           .Select(u=>ZodiacPRO.Models.UserProfile.GetUserProfile(u))
                           .OrderBy(u=>u.FamilyName);


return View(orderedPatients);

または :

var orderedPatients = from u in Roles.GetUsersInRole("user")
                      let userProfile = ZodiacPRO.Models.UserProfile.GetUserProfile(u)
                      order by userProfile.FamilyName
                      select userProfile;
return View(orderedPatients);
于 2012-07-19T15:19:56.757 に答える
1

それを変数に割り当てる必要があり、OrderByソートされたコレクションを返します。

pprofiles = pprofiles.OrderBy(x => x.familyName);
于 2012-07-19T15:19:15.853 に答える