我要做的是在我的对象中获取一个List<Feature>
,其中包含一系列功能。
我有两个主要类,用于保存有关项目的信息:
public class Feature
{
public int FeaturePackId { get; set; }
public string FeatureDescription { get; set; }
public int FeatureId { get; set; }
public bool AlwaysOn { get; set; }
}
public class FeaturePack
{
public int FeaturePackId { get; set; }
public List<Feature> Features { get; set; }
}
FeaturePack包中有ID和功能列表。
下面我将从GetPSTNFeaturePacksById()
获取Feautres列表然后迭代此列表并根据其ID将这些项目放入单独的列表中。从这里我有两个清单。 SelectFeature
列表包含IDs
大于0的功能列表。如何将这些功能添加到FeaturePack
FeaturePackID
Feature
内的ID
对象中等于FeaturePack
Features = CommonMethods.GetPSTNFeaturePacksById();
foreach (var item in Features)
{
if (item.FeaturePackId == 0)
{
DefaultFeatures.Add(new Feature
{
FeaturePackId = item.FeaturePackId,
FeatureDescription = item.FeatureDescription,
FeatureId = item.FeatureId,
AlwaysOn = item.AlwaysOn
});
}
else
{
SelectFeatures.Add(new Feature
{
FeaturePackId = item.FeaturePackId,
FeatureDescription = item.FeatureDescription,
FeatureId = item.FeatureId,
AlwaysOn = item.AlwaysOn
});
}
}
List<FeaturePack>
简而言之,我希望List<Feature>
包含FeaturePacks:
FeaturePackId 1
Features
FeaturePackId = 1
FeaturePackDescription = "lorum ipsum"
FeatureId = 1
AlwaysOn = True
/
FeaturePackId = 1
FeaturePackDescription = "lorum ipsum"
FeatureId = 1
AlwaysOn = True
FeaturePackId 2
Features
FeaturePackId = 2
FeaturePackDescription = "lorum ipsum"
FeatureId = 3
AlwaysOn = True
/
FeaturePackId = 2
FeaturePackDescription = "lorum ipsum"
FeatureId = 4
AlwaysOn = True
...
如何生成与此类似的输出?
var distinctGroups = (from z in SelectFeatures
orderby z.FeaturePackId
select z.FeaturePackId).Distinct();
foreach (var gp in distinctGroups)
{
foreach (var item in SelectFeatures)
{
if (item.FeaturePackId == gp)
{
FeaturePack.Add(new FeaturePack
{
FeaturePackId = gp,
Features = item.AsList()
});
}
}
}
修改
新尝试:
{{1}}
这是每个功能的FeaturePack的输出,我如何修改它以使相关功能在同一个包中?
答案 0 :(得分:3)
您需要一个简单的GroupBy
:
Features.GroupBy(f => f.FeaturePackId)
.Select(g => new FeaturePack{ FeaturePackId = g.Key,
Features = g.ToList() })
.ToList();
逐一打破这个:
Features.GroupBy(f => f.FeaturePackId)
收集Feature
个列表并将其分组到IEnumerable<IGrouping<Key, Value>>
,其中Key
是我们提供的函数的结果,在本例中为f => f.FeaturePackId
,并且Value
是我们正在使用的对象列表(在本例中为Feature
s)。在此声明之后,我们将Feature
的所有FeaturePackId
与一组中的FeaturePackId
放在一起,其中包含多个这些组(每 .Select(g => new FeaturePack{ FeaturePackId = g.Key,
Features = g.ToList() })
.ToList();
一个)。
Feature
然后,此选择将获取每组FeaturePack
并从中创建f => f.FeaturePackId
。组的关键是上面的函数Feature
的结果,组本身就是Select
的列表。此IEnumerable<FeaturePack>
的结果是FeaturePack
,其中每个Features
都填充了ToList()
列表。对IEnumerable<FeaturePack>
的最终通话会将List<FeaturePack>
转换为{{1}}。
答案 1 :(得分:1)
你应该这样做:
from z in SelectFeatures
order z by z.FeaturePackId into g
select new FeaturePack {
FeaturePackId = g.Key,
Features = g}
public class FeaturePack
{
public int FeaturePackId { get; set; }
public IEnumerable<Feature> Features { get; set; }
}
答案 2 :(得分:1)
开始学习LINQ,但它可以帮助你,但它是匿名类型:
var SelectedFeatures_lookup = SelectedFeatures.ToLookUp(p=>p.FeaturePackId);
var query = from feature in SelectedFeatures
group feature by feature.FeaturePackId into g
select new
{
FeaturePackId = g.Key,
Features = g
};
foreach(var q in query)
{
Console.WriteLine("FeaturePack = " + q.FeaturePackId);
Console.WriteLine("Features: ");
(foreach var qq in q.Features)
{
Console.WriteLine("\t " + qq.FeaturePackId);
Console.WriteLine("\t " + ...);
}
}
如果不起作用,请尝试给我答案
答案 3 :(得分:1)
这应该适合你:
var SelectFeatures = Features.Where(item => item.FeaturePackId != 0) .GroupBy(item => item.FeaturePackId) .Select(grp => new FeaturePack { FeaturePackId = grp.Key, Features = grp.ToList() }) .ToList(); var DefaultFeatures = Features.Where(item => item.FeaturePackId == 0).ToList();