Querying a Dictionary with a Lambda Expression

本小妞迷上赌 提交于 2021-02-08 09:16:07

问题


I've created some sample code below, and am trying to use a lambda expression to query against the SoftwareComponents Dictionary. The problem is that the query returns a var of type IGrouping, when what I need to do is further refine the query so that it returns a type of IGrouping where the first string is the SoftwareComponent.ComponentName, and the second string is the SoftwareComponent.ComponentDescription. Anyone know how to do this?

I was hoping the data returned would look something like: "New Type Description" "component1" "component2" "Old Type Description" "component3" "component4"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{

    protected void Page_Load(object sender, EventArgs e)
    {
        UnOwnedSoftware software = new UnOwnedSoftware();

        var components = software.SoftwareComponents.Values.
            GroupBy(s => s.ComponentName);
    }
}

public class UnOwnedSoftware
{
    public Dictionary<int, SoftwareComponent> SoftwareComponents
        = new Dictionary<int, SoftwareComponent>();

    public UnOwnedSoftware()
    {
        SoftwareComponent component1 = new SoftwareComponent
            ("component1", 1, "New Type Description");
        SoftwareComponent component2 = new SoftwareComponent
            ("component2", 2, "New Type Description");
        SoftwareComponent component3 = new SoftwareComponent
            ("component3", 3, "Old Type Description");
        SoftwareComponent component4 = new SoftwareComponent
            ("component4", 4, "Old Type Description");

        SoftwareComponents.Add(1, component1);
        SoftwareComponents.Add(2, component2);
        SoftwareComponents.Add(3, component3);
        SoftwareComponents.Add(4, component4);
    }   
}

public class SoftwareComponent
{
    public string ComponentName { get; set; }
    public int ID { get; set; }
    public string ComponentDescription { get; set; }

    public SoftwareComponent(string componentName, int id, string componentDescription)
    {
        ComponentName = componentName;
        ID = id;
        ComponentDescription = componentDescription;
    }
}

回答1:


Try this:

var components = (from s in software.SoftwareComponents.Values
                  select new
                  {
                      Name = s.ComponentName,
                      Description = s.ComponentDescription
                  })
                 .ToList().GroupBy(s=>s.Name);



回答2:


Given the sample data you provided, grouping by ComponentName is not going to result in any useful groups. I am not sure if your data actually has a unique name for each component, but if there are, then grouping won't actually provide any value.

To actually achieve the grouping you need, however, you can do the following:

var components = from v in software.SoftwareComponents.Values
                 group v by v.name into g
                 select new { ComponentName = g.Key, Description = g.First(v => v.Description);

That should result in an enumeration of components with the ComponentName and Description. Note that the only way to retrieve values from a group is to either select the first or last entry, or perform an aggregate sum, avg, etc. The only directly selectable value is the key (which may be composite, and therefor have multiple values.)



来源:https://stackoverflow.com/questions/1013440/querying-a-dictionary-with-a-lambda-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!