0

我有一个动作可以加载一个字符串数组,这些字符串由页面上的相应复选框选中:

        public ActionResult Test1(string[] checked_strings)

我想使用与我想动态创建 groupby 语句的实体名称相同的字符串。

这将是这样的:

            bool bChecked = Request.Form["checked_strings"].Contains("true");
            cmpqry =   from x in checked_strings
                       from y in typeof(Company).GetProperties()
                       where y.Name == x.ToString()
     // Create the groupby statement for the properties matched with the strings //

知道如何解决这个问题吗?

cmpqry 保存模型类的 IEnumarable 列表(稍后返回查看)。

4

2 回答 2

2

这是有效的代码示例 - 您应该能够按照您希望它们分组的顺序提供要分组的字段:

public class Company
{
    public int CompanyID { get; set; }
    public string CompanyName { get; set; }
    public string SubBranchName { get; set; }
    public string ProductName { get; set; }
}

public static class MyEnumerableExtensions
{
    /// <summary>
    /// Applies grouping to the collection of elements using the selectors specified
    /// </summary>
    /// <typeparam name="TElement">Type of the element</typeparam>
    /// <param name="elements">Elements to be grouped</param>
    /// <param name="groupSelectors">Selectors, or properties to be grouped on</param>
    /// <returns></returns>
    public static IEnumerable<GroupResult> GroupByMany<TElement>(
        this IEnumerable<TElement> elements,
        params Func<TElement, object>[] groupSelectors)
    {
        if (groupSelectors.Length > 0)
        {
            var selector = groupSelectors.First();

            //reduce the list recursively until zero
            var nextSelectors = groupSelectors.Skip(1).ToArray();
            return
                elements.GroupBy(selector).Select(
                    g => new GroupResult
                    {
                        Key = g.Key,
                        Items = g,
                        SubGroups = g.GroupByMany(nextSelectors)
                    });
        }
        return null;
    }

    public class GroupResult
    {
        public object Key { get; set; }
        public IEnumerable Items { get; set; }
        public IEnumerable<GroupResult> SubGroups { get; set; }
    }
}



//Your usage of the GroupByMany
IEnumerable<Company> list = new List<Company>
 {new Company{CompanyID = 1, CompanyName = "Company1", ProductName = "Product1", SubBranchName = "SB1"},
 new Company{CompanyID = 2, CompanyName = "Company1", ProductName = "Product2", SubBranchName = "SB2"},
 new Company{CompanyID = 3, CompanyName = "Company1", ProductName = "Product3", SubBranchName = "SB1"},
 new Company{CompanyID = 4, CompanyName = "Company2", ProductName = "Product4", SubBranchName = "SB2"},
 new Company{CompanyID = 5, CompanyName = "Company2", ProductName = "Product1", SubBranchName = "SB2"},
 new Company{CompanyID = 6, CompanyName = "Company2", ProductName = "Product2", SubBranchName = "SB1"},
 new Company{CompanyID = 7, CompanyName = "Company2", ProductName = "Product3", SubBranchName = "SB2"},
 new Company{CompanyID = 8, CompanyName = "Company3", ProductName = "Product4", SubBranchName = "SB2"},
 new Company{CompanyID = 9, CompanyName = "Company3", ProductName = "Product3", SubBranchName = "SB1"},
 new Company{CompanyID = 10, CompanyName = "Company3", ProductName = "Product2", SubBranchName = "SB2"},
 };

    var groupedByProductNameThenSubBranchName = list.GroupByMany(p => p.ProductName, p => p.SubBranchName);

    foreach (var groupResult in groupedByProductNameThenSubBranchName)
    {
        foreach (var result in groupResult.SubGroups)
        {
            foreach (var groupResult1 in result.Items)
            {
                Company company = groupResult1 as Company;
                Debug.Print(String.Format("ProductName: {0}, SubBranchName: {1}", company.ProductName, company.SubBranchName));
            }
        }
    }

    var groupedBySubBranchNameThenCompany = list.GroupByMany(p => p.SubBranchName, p => p.CompanyName);

    foreach (var groupResult in groupedBySubBranchNameThenCompany)
    {
        foreach (var result in groupResult.SubGroups)
        {
            foreach (var groupResult1 in result.Items)
            {
                Company company = groupResult1 as Company;
                Debug.Print(String.Format("SubBranchName: {0}, CompanyName: {1}", company.SubBranchName, company.CompanyName));
            }
        }
    }

第一个“foreach”打印出来

ProductName: Product1, SubBranchName: SB1
ProductName: Product1, SubBranchName: SB2
ProductName: Product2, SubBranchName: SB2
ProductName: Product2, SubBranchName: SB2
ProductName: Product2, SubBranchName: SB1
ProductName: Product3, SubBranchName: SB1
ProductName: Product3, SubBranchName: SB1
ProductName: Product3, SubBranchName: SB2
ProductName: Product4, SubBranchName: SB2
ProductName: Product4, SubBranchName: SB2

第二个“foreach”打印出来

SubBranchName: SB1, CompanyName: Company1
SubBranchName: SB1, CompanyName: Company1
SubBranchName: SB1, CompanyName: Company2
SubBranchName: SB1, CompanyName: Company3
SubBranchName: SB2, CompanyName: Company1
SubBranchName: SB2, CompanyName: Company2
SubBranchName: SB2, CompanyName: Company2
SubBranchName: SB2, CompanyName: Company2
SubBranchName: SB2, CompanyName: Company3
SubBranchName: SB2, CompanyName: Company3

希望这可以帮助!

于 2013-03-03T22:21:09.473 回答
1

我不能确切地说这与您的特定问题有多大的相关性,但如果您有一个字段要对您的数据进行分组,您可以编写类似于以下内容的查询,它将博客文章按创建年份分组

var results = from allPosts in db.Posts.OrderBy(p => p.DateCreated)
              group allPosts by allPosts.DateCreated.Year into postsByYear;

这里的“结果”是组的枚举——在这种情况下,是某一年发布的帖子。

这可以进一步扩展以在“第一级”内创建子组 - 按月份组发布的帖子类似于

var results = from allPosts in db.Posts.OrderBy(p => p.DateCreated)
              group allPosts by allPosts.DateCreated.Year into postsByYear

              select new
              {
                  postsByYear.Key,
                  SubGroups = from yearLevelPosts in postsByYear
                              group yearLevelPosts by yearLevelPosts.DateCreated.Month into postsByMonth;
              };

现在 SubGroups 是一个月内帖子的枚举。这里有更多信息(可能很有帮助)

http://blogs.msdn.com/b/mitsu/archive/2007/12/22/playing-with-linq-grouping-groupbymany.aspx

在这里(我将上一个链接应用到我遇到的特定问题)

http://www.ynegve.info/Post/156/implementing-a-tree-view-small-case-study

如果这看起来像您需要的,但您仍然无法使其工作,请发布有关您的模型的更多信息。

于 2013-03-03T10:28:50.947 回答