简体   繁体   English

Linq - 集团的第一个名字

[英]Linq - Group by first letter of name

I'm using Entity Framework 6 as my data source. 我正在使用Entity Framework 6作为我的数据源。

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. 我创建了一个页面,用他们名字的第一个字母列出供应商,所以首先我们有'A',然后'B'等等。

In order to do this, I use 2 ListView objects - it could easily be a Repeater, but that's not important. 为了做到这一点,我使用2个ListView对象 - 它可以很容易地成为一个Repeater,但这并不重要。

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. 虽然我的供应商列表并不广泛,但我用来获取数据的方法非常昂贵,因为在数据绑定期间我必须将其称为27次。 I'm pretty sure there is a better way of going about this but don't know my way around Linq well enough. 我很确定有更好的方法可以解决这个问题,但我不知道我在Linq周围的方式。

I'm thinking there must be a way of grouping data and then looping through the content of a group. 我认为必须有一种方法来分组数据,然后循环遍历组的内容。

Here is the important bits of code. 这是重要的代码。 The linq to retreive the child data and the databinding code: linq用于检索子数据和数据绑定代码:

    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)

The above code should group all the data in a single iteration. 上面的代码应该在一次迭代中对所有数据进行分组。 The code works for LINQ to objects. 该代码适用于LINQ对象。 It should work the same way for LINQ to entities also. 对于LINQ实体也应该以相同的方式工作。

I'm thinking there must be a way of grouping data and then looping through the content of a group. 我认为必须有一种方法来分组数据,然后循环遍历组的内容。

Yes there are. 是的,有。 Try something like following 尝试以下内容

Select().GroupBy(c=>string.IsNullOrEmpty(c.Name) ? '' : c.Name[0]);

Just added string.IsNullOrEmpty to make sure string is not null. 刚添加string.IsNullOrEmpty以确保字符串不为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM