Linq查询返回相同的名称,即使它们不同 [英] Linq query returns the same names even though they should be different

查看:328
本文介绍了Linq查询返回相同的名称,即使它们不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是EF6的新手,我已经设置了Chinook数据库,并且在.NET Framework 4.0上使用SqlLite .NET提供程序。



当我执行以下查询,它执行没有问题,但轨道名称都是一样的。它们应该是不同的,因为它们具有不同的轨迹ID,并且我查找了这些轨道ID,并且它们具有不同的名称。

  var result = context.Playlists.Include(p => p.Tracks)
.Where(p => p.Name ==Brazilian Music)
.SelectMany(p => p.Tracks );

foreach(结果中的var p)
{
Console.WriteLine(p.Playlist.Name +,+ p.TrackId +,+ p.Track。名称);
}

欣赏任何帮助。



这是我的结果的输出:



控制台输出:



解决方案

好的,我尝试了另一种方法,使用连接连接表,现在它给出正确的结果

  var result = from p in context.Playlists 
join pt in p.PlaylistId上的context.PlaylistTracks等于pt.PlaylistId
在上下文中加入t.trackId上的跟踪等于t.TrackId
其中p.Name ==Brazilian Music
select new { PlaylistName = p.Name,TrackId = t.TrackId,TrackName = t.Name};

foreach(结果中的var p)
{
Console.WriteLine(p.PlaylistName +,+ p.TrackId +,+ p.TrackName);
}

我已经检查了连接表POCO,我没有看到任何问题



我仍然不明白为什么第一种方法不起作用。


I am new to EF6 and I have set up the Chinook database and gotten it working with SqlLite .NET provider on .NET Framework 4.0.

When I execute the following query, it executes without problems but the track names are all the same. They should be different because they have different track IDs and I have looked up those track IDs and they have different names.

var result = context.Playlists.Include(p => p.Tracks)
                .Where(p => p.Name == "Brazilian Music")
                .SelectMany(p => p.Tracks);

foreach(var p in result)
{
  Console.WriteLine(p.Playlist.Name + ", " + p.TrackId + ", " + p.Track.Name);
}

Appreciate any help.

Here is the output of my result:

Console Output:

解决方案

OK, I tried another approach using joins to the junction table and now it gives the correct result

var result = from p in context.Playlists
             join pt in context.PlaylistTracks on p.PlaylistId equals pt.PlaylistId
             join t in context.Tracks on pt.TrackId equals t.TrackId
             where p.Name == "Brazilian Music"
             select new { PlaylistName = p.Name, TrackId = t.TrackId, TrackName = t.Name };

foreach (var p in result)
{
  Console.WriteLine(p.PlaylistName + ", " + p.TrackId + ", " + p.TrackName);
}

I have checked the junction table POCO and I don't see any problems with it.

I still don't understand why the first approach isn't working.

这篇关于Linq查询返回相同的名称,即使它们不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆