将从不同来源收集的数据合并到一个 IEnumerable 对象中
Combine data gathered from different sources into one IEnumerable object
我下面有这个方法,它从不同的来源收集数据,returns它作为一个 IEnumerable。
不过我遇到了麻烦,想弄清楚如何将所有源合并到一个类型为 TotalRoomContents 的对象中。
TotalRoomContents 的类型为 IEnumerable<String>
。
这里是未完成的方法:
public static TotalRoomContents GetRoomContents(this Dungeon dungeon)
{
var customArmor = dungeon.Rooms
.Select(pe => pe.Room)
.Where(e => e.Monster.IsActive);
// check each customArmor object to see if it exists in the MapLookupByRoomId dictionary
if (customArmor != null && MapLookupByRoomId.ContainsKey(customArmor.Id))
// add all item(s) of type customArmor to TotalRoomContents()
if(dungeon.RoomType?.InventoryContent != null)
{
// add item(s) from dungeon.RoomType?.InventoryContent to TotalRoomContents()
}
return new TotalRoomContents()
}
如您所见,我不知道如何将项目添加到 TotalRoomContents
对象。
项目将来自 dungeon.RoomType?.InventoryContent
和在 linq 查询中找到的所有 customArmor
对象。
有没有一种方法可以做到这一点,或者我是否需要创建某种类型的其他方法来做到这一点?
谢谢!
您可以创建一个包装器 class 来处理这个问题。一个可能的实现看起来像这样
public class AggregateEnumerable<T> : IEnumerable<T> {
private readonly IEnumerable<T>[] _sources;
public AggregateEnumerable( params IEnumerable<T>[] sources ) {
_sources = sources;
}
public IEnumerator<T> GetEnumerator() {
foreach( var source in _sources ) {
var enumerator = source.GetEnumerator();
while( enumerator.MoveNext() )
yield return enumerator.Current;
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
然后你会像
一样使用它
var firstEnumerable = new[] { "Peter", "John" };
var secondEnumerable = new[] { "Thomas", "George" };
var myEnum = new AggregateEnumerable<string>(firstEnumerable, secondEnumerable);
foreach( var value in myEnum )
Console.WriteLine(value);
您为什么不创建一个 "RoomContent" 的列表(代表一个房间可以包含的任何内容),然后开始添加来自其他查询的所有不同结果?
List<RoomContent> TotalRoomContents = new List<RoomContent>();
if (/* Whatever condition needs to be met */)
{
TotalRoomContents.Add(/* Whatever you may want */);
}
此外,您应该知道 Linq 查询在代码需要枚举之前不会执行,因此,基本上您可以按以下步骤构建查询:
// This is just to simulate the data source
IQueryable<RoomContent> query = allPossibleRoomContents.AsQueryable();
query = query.Where(x => x.ContentDescription = "Sword");
query = query.Where(x => x.ContentDescription = "Axe");
// This is where the actual work is done
return query.ToList();
希望对您有所帮助!
我下面有这个方法,它从不同的来源收集数据,returns它作为一个 IEnumerable。
不过我遇到了麻烦,想弄清楚如何将所有源合并到一个类型为 TotalRoomContents 的对象中。
TotalRoomContents 的类型为 IEnumerable<String>
。
这里是未完成的方法:
public static TotalRoomContents GetRoomContents(this Dungeon dungeon)
{
var customArmor = dungeon.Rooms
.Select(pe => pe.Room)
.Where(e => e.Monster.IsActive);
// check each customArmor object to see if it exists in the MapLookupByRoomId dictionary
if (customArmor != null && MapLookupByRoomId.ContainsKey(customArmor.Id))
// add all item(s) of type customArmor to TotalRoomContents()
if(dungeon.RoomType?.InventoryContent != null)
{
// add item(s) from dungeon.RoomType?.InventoryContent to TotalRoomContents()
}
return new TotalRoomContents()
}
如您所见,我不知道如何将项目添加到 TotalRoomContents
对象。
项目将来自 dungeon.RoomType?.InventoryContent
和在 linq 查询中找到的所有 customArmor
对象。
有没有一种方法可以做到这一点,或者我是否需要创建某种类型的其他方法来做到这一点?
谢谢!
您可以创建一个包装器 class 来处理这个问题。一个可能的实现看起来像这样
public class AggregateEnumerable<T> : IEnumerable<T> {
private readonly IEnumerable<T>[] _sources;
public AggregateEnumerable( params IEnumerable<T>[] sources ) {
_sources = sources;
}
public IEnumerator<T> GetEnumerator() {
foreach( var source in _sources ) {
var enumerator = source.GetEnumerator();
while( enumerator.MoveNext() )
yield return enumerator.Current;
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
然后你会像
一样使用它var firstEnumerable = new[] { "Peter", "John" };
var secondEnumerable = new[] { "Thomas", "George" };
var myEnum = new AggregateEnumerable<string>(firstEnumerable, secondEnumerable);
foreach( var value in myEnum )
Console.WriteLine(value);
您为什么不创建一个 "RoomContent" 的列表(代表一个房间可以包含的任何内容),然后开始添加来自其他查询的所有不同结果?
List<RoomContent> TotalRoomContents = new List<RoomContent>();
if (/* Whatever condition needs to be met */)
{
TotalRoomContents.Add(/* Whatever you may want */);
}
此外,您应该知道 Linq 查询在代码需要枚举之前不会执行,因此,基本上您可以按以下步骤构建查询:
// This is just to simulate the data source
IQueryable<RoomContent> query = allPossibleRoomContents.AsQueryable();
query = query.Where(x => x.ContentDescription = "Sword");
query = query.Where(x => x.ContentDescription = "Axe");
// This is where the actual work is done
return query.ToList();
希望对您有所帮助!