Linq-按名字的首字母分组 [英] Linq - Group by first letter of name

查看:454
本文介绍了Linq-按名字的首字母分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 6作为数据源.

I'm using Entity Framework 6 as my data source.

我创建了一个页面,该页面将按供应商名称的第一个字母列出供应商,因此首先我们拥有所有的'A',然后是'B',依此类推.

I've created a page that will list suppliers by the first letter of their name, so first we have all the 'A's, then 'B's and so on.

为此,我使用了2个ListView对象-它可以很容易地成为Repeater,但这并不重要.

In order to do this, I use 2 ListView objects - it could easily be a Repeater, but that's not important.

尽管我的供应商列表不多,但我用来获取数据的方法却非常昂贵,因为在数据绑定期间我必须调用27次.我很确定有更好的方法来解决这个问题,但我对Linq的了解还不够.

Although my list of suppliers is not extensive, the method I've used to get the data is quite expensive in that I have to call it 27 times during databinding. I'm pretty sure there is a better way of going about this but don't know my way around Linq well enough.

我认为必须存在一种将数据分组然后遍历组内容的方法.

I'm thinking there must be a way of grouping data and then looping through the content of a group.

这是代码的重要部分.检索子数据和数据绑定代码的linq:

Here is the important bits of code. The linq to retreive the child data and the databinding code:

    public static IEnumerable<Supplier> StartsWith(string firstLetter)
    {
        return Select() // select simply returns all data for the entity
            .Where(x => x.Name.StartsWith(firstLetter, StringComparison.OrdinalIgnoreCase));
    }

    protected void ListViewAtoZ_ItemDataBound(object source, ListViewItemEventArgs e)
    {
        var item = e.Item;

        if (item.ItemType == ListViewItemType.DataItem)
        {
            var alphanumeric = (string)item.DataItem;

            var h2 = item.GetControl<HtmlGenericControl>("HtmlH2", true);
            h2.InnerText = alphanumeric;

            var childView = item.GetControl<ListView>("ListViewStartsWith", true);
            childView.DataSource = LenderView.StartsWith(alphanumeric);
            childView.DataBind();
        }
    }

    protected void ListViewStartsWith_ItemDataBound(object source, ListViewItemEventArgs e)
    {
        var item = e.Item;

        if (item.ItemType == ListViewItemType.DataItem)
        {
            var supplier = (Supplier)item.DataItem;

            var litName = item.GetControl<Literal>("LiteralName", true);
            litName.Text = supplier.Name;
        }
    }

    void LoadData()
    {
        var alphanumerics = new string[]
        {
            "0 - 9","A","B","C","D","E","F","G","H","I","J","K","L",
            "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
        }; 

        ListViewAtoZ.DataSource = alphanumerics;
        ListViewAtoZ.DataBind();
    }

推荐答案

您可以使用以下内容对项目进行分组并获得子列表.

You can use the following to group and get the sub list of items.

Select().GroupBy(x => x.Name.Substring(0,1).ToUpper(), (alphabet, subList) => new { Alphabet = alphabet, SubList = subList.OrderBy(x => x.Name).ToList() })
                .OrderBy(x => x.Alphabet)

以上代码应在一次迭代中将所有数据分组.该代码适用于LINQ对象. LINQ对实体也应该以相同的方式工作.

The above code should group all the data in a single iteration. The code works for LINQ to objects. It should work the same way for LINQ to entities also.

这篇关于Linq-按名字的首字母分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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