在列表中查找项目<基于id
本文关键字:基于 id 项目 查找 列表 | 更新日期: 2023-09-27 18:14:36
我要做的是将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()
的功能列表,然后我遍历该列表,并根据它们的ID将项目放入单独的列表中。从这里我有两个列表。SelectFeature
列表包含IDs
大于0的特征列表。我如何将这些添加到FeaturePack
对象中,其中Feature
中的FeaturePackID
等于FeaturePack
的ID
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<Feature>
的List<FeaturePack>
我怎样才能产生类似的输出?
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()
});
}
}
}
这个的输出是每个特性的一个FeaturePack,我如何修改它,使相关的特性在同一个包中?
您需要一个简单的GroupBy
:
Features.GroupBy(f => f.FeaturePackId)
.Select(g => new FeaturePack{ FeaturePackId = g.Key,
Features = g.ToList() })
.ToList();
逐条分解:
Features.GroupBy(f => f.FeaturePackId)
获取Feature
s列表并将它们分组为IEnumerable<IGrouping<Key, Value>>
,其中Key
是我们提供的函数的结果,在本例中为f => f.FeaturePackId
, Value
是我们正在处理的对象的列表(在本例中为Feature
)。在此语句之后,我们将所有Feature
s与相同的FeaturePackId
一起放在一个组中,其中有几个组(每个FeaturePackId
一个)。
.Select(g => new FeaturePack{ FeaturePackId = g.Key,
Features = g.ToList() })
.ToList();
这个select然后获取每组Feature
并从中创建一个FeaturePack
。组的键是上面的函数f => f.FeaturePackId
的结果,而组本身是我们的Feature
列表。Select
的结果是IEnumerable<FeaturePack>
,其中每个FeaturePack
都填充了它的Features
列表。最后对ToList()
的调用将IEnumerable<FeaturePack>
转换为List<FeaturePack>
。
你应该这样做:
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; }
}
我开始学习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 " + ...);
}
}
请试着给我答案,如果不工作
这应该可以为您工作:
<>之前var SelectFeatures =功能。其中(item => item。= 0).GroupBy(item => item. featurepackid).选择(grp => new FeaturePack {FeaturePackId = grp;Key, Features = grp.ToList()}).ToList ();var DefaultFeatures =功能。其中(item => item。FeaturePackId == 0).ToList();