使用 Lambda 表达式查询字典
我在下面创建了一些示例代码,并尝试使用 lambda 表达式来查询 SoftwareComponents 字典。 问题是查询返回 IGrouping 类型的 var,而我需要做的是进一步细化查询,以便它返回 IGrouping 类型,其中第一个字符串是 SoftwareComponent.ComponentName,第二个字符串是 SoftwareComponent。组件描述。 有人知道怎么做吗?
我希望返回的数据看起来像这样: 《新类型说明》 “组件1” “组件2” “旧类型描述” “组件3” “组件4”
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个:
Try this:
根据您提供的示例数据,按 ComponentName 分组不会产生任何有用的组。 我不确定您的数据是否确实为每个组件都有唯一的名称,但如果有,那么分组实际上不会提供任何价值。
然而,要实际实现所需的分组,您可以执行以下操作:
这应该会生成带有 ComponentName 和 Description 的组件枚举。 请注意,从组中检索值的唯一方法是选择第一个或最后一个条目,或者执行聚合总和、平均值等。唯一可直接选择的值是键(可能是复合值,因此具有多个值) .)
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:
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.)