需要 Char[] 数组帮助
我有一个数据库中包含公司名称的对象集合。
我需要获取所有不同标题的第一个字母 (char[]) (.Distinct())。
目前我有类似的东西:
public char[] GetBrandsAlphabet()
{
using (var dc = ReturnContext())
{
var resultBrands =
from brands in dc.Brands
orderby brands.Title ascending
select brands.Title.ToCharArray()[0];
char[] ret = new char[resultBrands.Distinct().Count()];
}
}
I have a collection of objects from a database that holds the titles of companies.
I need to get the first letters (char[]) of all the titles distincted (.Distinct()).
Currently I have something like:
public char[] GetBrandsAlphabet()
{
using (var dc = ReturnContext())
{
var resultBrands =
from brands in dc.Brands
orderby brands.Title ascending
select brands.Title.ToCharArray()[0];
char[] ret = new char[resultBrands.Distinct().Count()];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将最后一行替换为:
或者,嘿,用这个衬垫替换其他所有内容怎么样?
ToUpper
保证最终数组中不会同时包含“A”和“a”。Replace the last line with this:
Or, hey, how about this one liner to replace everything else?
The
ToUpper
guarantees that you won't have both 'A' and 'a' in your final array.但是,这可能无法完全按照您想要的方式工作,因为原则上
Distinct
可以更改元素的顺序。我将从 sql 语句中删除 orderby 语句并使用:However, this might not work exactly the way that you want, since
Distinct
could, in principle, change the ordering of the elements. I would remove theorderby
statement from the sql statement and use:好的其他人已经说过了。不过,您可以改进这部分:
字符串已经可以像字符数组一样通过索引寻址,您不需要转换它。
OK What others already said. You can improve this part though:
A string is already addressable by index like a char array, you don't need to convert it.