Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将3个不同类型的列表合并到一个新的列表类型中_C#_Linq_Concat - Fatal编程技术网

C# 将3个不同类型的列表合并到一个新的列表类型中

C# 将3个不同类型的列表合并到一个新的列表类型中,c#,linq,concat,C#,Linq,Concat,拥有以下模型(为了简单起见,我发布了接口) 然后,我在另一个ViewModel中创建了每种类型的ObservableCollection public ObservableCollection<LengthViewModel> Lengths { get; set; } public ObservableCollection<SlopeViewModel> Slopes { get; set; } public ObservableCollection<Rating

拥有以下模型(为了简单起见,我发布了接口)

然后,我在另一个ViewModel中创建了每种类型的ObservableCollection

public ObservableCollection<LengthViewModel> Lengths { get; set; }
public ObservableCollection<SlopeViewModel> Slopes { get; set; }
public ObservableCollection<RatingViewModel> Ratings { get; set; }
publicobservableCollection长度{get;set;}
公共可观测收集斜率{get;set;}
公共可观察收集评级{get;set;}
我需要将上面的列表转换为一个列表,下面是应该创建的新列表类型

public ObservableCollection<LengthSlopeRatingViewModel> Aggregate { get; set; }

public class LengthSlopeRatingViewModel
{
    public string Category { get; set; }
    public int Length { get; set; }
    public int Slope { get; set; }
    public double Rating { get; set;}
}
publicobservableCollection聚合{get;set;}
公共类LengthSlopeRatingViewModel
{
公共字符串类别{get;set;}
公共整数长度{get;set;}
公共整数斜率{get;set;}
公共双重评级{get;set;}
}
到目前为止,我一直在尝试,但似乎一直无法解决如何为每个转换列表选择属性的问题

var lengths = Lengths.Select(p => new LengthSlopeRatingViewModel
{
    Category = p.Category,
    Length = p.Length
});

var slopes = Slopes.Select(p => new LengthSlopeRatingViewModel
{
    Category = p.Category,
    Slope = p.Slope
});

var ratings = Ratings.Select(p => new LengthSlopeRatingViewModel
{
    Category = p.Category,
    Rating = p.Rating
});

// Concat and group them, then select new type again with the properties?
CourseRatings = lengths.Concat(slopes)
    .Concat(ratings)
    .GroupBy(p => p.Category)
    .Select(g => g.ToList())
    .As<IEnumerable<LengthSlopeRatingViewModel>>()
    .ToObservableCollection(); 
var length=length.选择(p=>new LengthSlopeRatingViewModel
{
类别=p.类别,
长度=p.长度
});
变量斜率=斜率。选择(p=>new LengthSlopeRatingViewModel
{
类别=p.类别,
坡度=p.坡度
});
var评级=评级。选择(p=>new LengthSlopeRatingViewModel
{
类别=p.类别,
评级=p.评级
});
//Concat并将其分组,然后再次选择具有属性的新类型?
层数=长度。混凝土(坡度)
.Concat(评级)
.GroupBy(p=>p.Category)
.Select(g=>g.ToList())
.As()
.ToObservableCollection();

例如,如果您有一个实例具有长度、坡度和等级,并且
Category=“Black”
和另一个实例具有
Category=“Blue”
我应该得到两个LengthSlopeRatingViewModel实例,一个具有
Category=“Black”
和第一个匹配的对应值,另一个具有
Category=“Blue”

可以使用s将多个列表按一个通用标准进行组合

如果仅当
所有集合都包含一个对应类别项时,才需要
长度坡度运行视图模型
结果:

Aggregate = new ObservableCollection<LengthSlopeRatingViewModel>(
    from l in Lengths
    join s in Slopes on l.Category equals s.Category
    join r in Ratings on s.Category equals r.Category
    select new LengthSlopeRatingViewModel {
        Category = l.Category,
        Length = l.Length,
        Slope = s.Slope,
        Rating = r.Rating
    });

GroupBy
之后的
Select
不应该是
SelectMany
?提问时,请在设置代码格式方面投入更多的精力-大多数代码不需要缩进到右边很远的地方。这次我已经修改了这个问题。老实说,听起来你真正想要的是一些连接。。。你还应该说,如果该类别仅在一个或两个原始集合中,你希望发生什么。我承认它需要更多的工作。我现在看到了加入,我有点卡住了,感谢编辑:-)我想知道谁是那个无缘无故投了反对票的人!谢谢你的详细回答,正是我所需要的!
Aggregate = new ObservableCollection<LengthSlopeRatingViewModel>(
    from l in Lengths
    join s in Slopes on l.Category equals s.Category
    join r in Ratings on s.Category equals r.Category
    select new LengthSlopeRatingViewModel {
        Category = l.Category,
        Length = l.Length,
        Slope = s.Slope,
        Rating = r.Rating
    });
Aggregate = new ObservableCollection<LengthSlopeRatingViewModel>(
    from l in Lengths
    join s in Slopes on l.Category equals s.Category
    join r in Ratings on s.Category equals r.Category into ratings
    from r in ratings.DefaultIfEmpty()
    select new LengthSlopeRatingViewModel {
        Category = l.Category,
        Length = l.Length,
        Slope = s.Slope,
        Rating = r?.Rating ?? 0
    });
Aggregate = new ObservableCollection<LengthSlopeRatingViewModel>(
    Lengths
    .Join(Slopes, _ => _.Category, _ => _.Category,
        (l, s) => new LengthSlopeRatingViewModel
            { Category = l.Category, Length = l.Length, Slope = s.Slope })
    .Join(Ratings, _ => _.Category, _ => _.Category,
        (ls, r) => { ls.Rating = r.Rating; return ls; }));
var categories =
    Lengths.Select(_ => _.Category).Concat(
    Slopes.Select(_ => _.Category)).Concat(
    Ratings.Select(_ => _.Category))
    .Distinct();

Aggregate = new ObservableCollection<LengthSlopeRatingViewModel>(
    from c in categories
    join l in Lengths  on c equals l.Category into lengths
    from l in lengths.DefaultIfEmpty()
    join s in Slopes on c equals s.Category into slopes
    from s in slopes.DefaultIfEmpty()
    join r in Ratings on c equals r.Category into ratings
    from r in ratings.DefaultIfEmpty()
    select new LengthSlopeRatingViewModel
    {
        Category = c,
        Length = l?.Length ?? 0, // or any other default
        Slope = s?.Slope ?? 0,
        Rating = r?.Rating ?? 0
    });