使用列表,匿名类型和诸如此类的工作 [英] Working with Lists, Anonymous Types and Suchlike

查看:165
本文介绍了使用列表,匿名类型和诸如此类的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从的前面的问题,我现在有一个匿名类型

Following on from an earlier question, I now have a collection of an anonymous type

[用户的集合:
的用户名(如 forename.surname
用户ID
]。

[User: Username (as forename.surname, UserId ].

的'用户'将最终需要是该集合绑定到一个下拉列表,这是好的,但我需要做的按姓氏和用的名字是有点儿他们。但这是一个事实,即用户名格式为forename.surname复杂。

This collection of 'users' will ultimately need to be bound to a dropdown list. This is fine, but what I need to do is sort them by Surname and forename. but this is complicated by the fact that the username format is forename.surname.

在一个高的水平,这将涉及拆分的字符串分隔名称组件,那么 ToTitleCase()两个部分,然后存储,我可以再排序使用列表℃的名单内的另一对象的新价值观; T> .OrderBy(...)ThenBy(...)

At a high-level, this will involve a Split on the string to separate the name components, then ToTitleCase() both parts, then store the new values in another object within a List which I can then sort using List<T>.OrderBy(...).ThenBy(...)

有发生,我认为这一切花哨的新语法,我试图了解可能包括几行执行此过程中的一种方式简洁的代码。任何人都可以证实或否认此

It occurred to me that all this fancy new syntax I'm attempting to learn might include a way of performing this process in a couple of lines of terse code. Can anyone confirm or deny this?

感谢

修改3:

我想我已经破解了:

var salesusers =    (
                      from s in lstReport 
                      group s by new { s.SalesUserId,s.Username} 
                      into g
                      select new
                          {
                             Username = g.Key.Username.Split('.')[1].ToTitleCase() + " " + g.Key.Username.Split('.')[0].ToTitleCase(),
                             Surname = g.Key.Username.Split('.')[1].ToTitleCase(),
                             Forename = g.Key.Username.Split('.')[0].ToTitleCase(),
                             UserId = g.Key.SalesUserId 
                           }
                     ).OrderBy(a=> a.Surname).ThenBy(a=> a.Forename);



我需要创建用户名单独的名字及姓氏字段排序目的和绑定的用户名到一个DropDownList。这似乎疯了,但它的工作这么好,我与它坚持现在。 。希望得到您的意见

I need to create the separate forename and surname fields from the Username for sorting purposes and the Username for binding to a dropdownlist. It seems insane but it works so nicely I'm sticking with it for now. Would appreciate your comments.

EDIT2:
所以我就尽可能这一点。现在,我想知道如果语法可以让我在集团通过操作从我刚才的问题,这一步..

So I got as far as this. Now I'm wondering if the syntax will allow me to combine the Group by operation from my earlier question with this step..

var sortedUsers = from u in salesusers
                  orderby u.UserName.Split('.')[1], u.UserName.Split('.')[0]
                  select new {UserName = u.UserName.Replace(".", " ").ToTitleCase(), UserId = u.UserId.Value};



有人...?

Anybody ...?

< STRONG>编辑:我能够做到的<击>所有大部分自己,万一有人是有史以来需要,但转换名称组件 ToTitleCase 排序操作过程中被证明是困难的。

I managed to do it all most of it myself, in case anyone is ever needing, but converting the name components ToTitleCase during the ordering operation is proving difficult.

这:

var sortedUsers = from u in salesusers
                  orderby u.UserName.Split('.')[1], u.UserName.Split('.')[0]
                  select u;



似乎做<罢工>的伎俩我需要的一切除了 ToTitleCase ING。不过,当然也有可能是一个更快/更简洁/更优雅的方式,所以我会离开这个开放一两天,看看有什么轮番上涨; - )

seems to do the trick everything I need apart from the ToTitleCaseing. But of course there may be an even quicker/terser/more elegant method so I'll leave this open for a day or two to see what turns up ;-)

推荐答案

这是我想出了一个最终的解决方案 - 它排序, ToTitleCase 和格式。而漂亮,如果我这样说自己。我花了整个上午寿: - 从s(

Here's the final solution I came up with - it sorts, ToTitleCases and formats. Rather nifty if I say so myself. Took me all morning tho :-(

var salesusers =    (
                        from s in lstReport 
                        group s by new { s.SalesUserId,s.Username} 
                        into g
                        select new
                            {
                                Username = g.Key.Username.Split('.')[1].ToTitleCase() + " " + g.Key.Username.Split('.')[0].ToTitleCase(),  
                                Surname = g.Key.Username.Split('.')[1].ToTitleCase(),  
                                Forename = g.Key.Username.Split('.')[0].ToTitleCase(),  
                                UserId = g.Key.SalesUserId 
                             }  
                     ).OrderBy(a=> a.Surname).ThenBy(a=> a.Forename);

这篇关于使用列表,匿名类型和诸如此类的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆