C# 循环在每次迭代中清空模型内容

C# 循环在每次迭代中清空模型内容,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我知道我错过了一些基本的东西。我有一个嵌套循环序列,可以从添加到特定播放列表的一组专辑中过滤出歌曲。这是我的密码: foreach (var item in model.Albums) { foreach (var song in model.PlaylistSongs) { model.Songs = item.Songs.Where(x => x.SongID == song.SongID).ToList(); foreach (var

我知道我错过了一些基本的东西。我有一个嵌套循环序列,可以从添加到特定播放列表的一组专辑中过滤出歌曲。这是我的密码:

foreach (var item in model.Albums)
{
    foreach (var song in model.PlaylistSongs)
    {
        model.Songs = item.Songs.Where(x => x.SongID == song.SongID).ToList();
        foreach (var single in model.Songs)
        {
            System.Diagnostics.Debug.WriteLine("Loop");
            System.Diagnostics.Debug.WriteLine(single.Album.AccountInfo.DisplayName);
        }
    }

}
其中:

model.Albums is List<Albums> // albums identified with a song used in the playlist
model.PlaylistSongs is List<PlaylistSongs> // list of all songs found in specific playlist
model.Songs is List<Songs> // resultant model
然而,我所看到的是这样的,因此模型在每次迭代中都会被抹去:

Loop
Koda
Loop
Danrell
Loop
Attom

有没有附加的方法?我尝试了
model.Songs.Add(item.Songs.Wherex=>x.SongID==song.SongID.ToList())
但它给了我一个异常
无法从System.Collections.Generic.List转换到Doman.Data.song。
您正在分配给
Songs
属性,而不是添加到它。所以每个循环都会得到一个新的副本

而不是

model.Songs = item.Songs.Where(x => x.SongID == song.SongID).ToList();
试一试

这是让你工作的最小改变。请注意,在开始循环之前必须实例化列表,例如
model.Songs=new list()

如果您希望它充分利用LINQ的潜力,那么您应该摆脱这个循环,让LINQ为您做这件事

而不是:

foreach (var item in model.Albums)
{
    foreach (var song in model.PlaylistSongs)
    {
        model.Songs = item.Songs.Where(x => x.SongID == song.SongID).ToList();
        foreach (var single in model.Songs)
        {
            System.Diagnostics.Debug.WriteLine("Loop");
            System.Diagnostics.Debug.WriteLine(single.Album.AccountInfo.DisplayName);
        }
    }
}
试试这个:

model.Songs = model.Albums
    .SelectMany       //Combine all albums songs into one list
    (
        a => a.Songs 
    )
    .Where           //Only take those that are in the playlist
    (
        s => model.PlaylistSongs.Any
        ( 
            p => p.SongID == s.SongID 
        )
    )
    .ToList();

foreach (var single in model.Songs)
{
    Debug.WriteLine(single.Album.AccountInfo.DisplayName);
}

你试过Addrange吗?您好,MongZhu刚刚按照Johns建议的答案进行了尝试。它给了我
System.NullReferenceException:“对象引用未设置为对象的实例”。
update,所以我尝试了这两种方法,带有循环的第一个选项为我提供了一个
System.NullReferenceException:“对象引用未设置为对象的实例”。
AddRange
行上,新的LINQ方法返回与我在原始问题中相同的结果。您还需要哪些信息来帮助解决问题@john在开始循环之前,您需要创建一个空列表以将项目放入其中,例如
model.Songs=new list()好,这样解决了实例错误,但现在输出了相同的错误结果(每个循环只显示一个艺术家)。再次用LINQ方法测试了一遍好吧,我很笨,我想我的调试循环错了。你说得对,谢谢!我不明白为什么我必须实例化一个空的新列表,因为在我的模型中,我已经设置了
公共列表歌曲。
foreach (var item in model.Albums)
{
    foreach (var song in model.PlaylistSongs)
    {
        model.Songs = item.Songs.Where(x => x.SongID == song.SongID).ToList();
        foreach (var single in model.Songs)
        {
            System.Diagnostics.Debug.WriteLine("Loop");
            System.Diagnostics.Debug.WriteLine(single.Album.AccountInfo.DisplayName);
        }
    }
}
model.Songs = model.Albums
    .SelectMany       //Combine all albums songs into one list
    (
        a => a.Songs 
    )
    .Where           //Only take those that are in the playlist
    (
        s => model.PlaylistSongs.Any
        ( 
            p => p.SongID == s.SongID 
        )
    )
    .ToList();

foreach (var single in model.Songs)
{
    Debug.WriteLine(single.Album.AccountInfo.DisplayName);
}