gpt4 book ai didi

c# - Linq select tablemapping( Entity Framework )

转载 作者:行者123 更新时间:2023-11-30 23:24:56 25 4
gpt4 key购买 nike

我有下表

**Track Table**
+---------------+---------------+--------------------+
| TrackId | TrackName | Path |
+===============+===============+====================+
| 1 | jb | http://domain/1.mp3|
+---------------+---------------+--------------------+
| 2 | remix | http://domain/2.mp3|
+---------------+---------------+--------------------+

**Favorite Table**
+---------------+---------------+--------------------+
| favoriteId | ProfileId | TrackId |
+===============+===============+====================+
| 10 | 2130 | 1 |
+---------------+---------------+--------------------+
| 11 | 2132 | 2 |
+---------------+---------------+--------------------+

我需要将轨道选择到一个模型中,我必须在其中包含一个 bool 字段 IsFavorite。现在我的逻辑如下:

1.Select Tracks
2.Select all favorites by profile
3.Iterate to both list and fill field "IsFavorite"

有没有更好的方法可以达到同样的效果?

以下是我目前的逻辑代码

Var ActiveTracks = jukeboxTrackService.GetActiveTracks();
var UserFavorites = jukeboxTrackService.GetFavoritesByProfile(ProfileId);
foreach (var item in ActiveTracks )
{
foreach (var fav in UserFavorites)
{
if (item.JukeBoxTrackId == fav.TrackId)
{
item.IsFavorite = true;
}
}
}

return ActiveTracks ;

提前致谢。

最佳答案

(from tr in ActiveTracks 
join usrfav in UserFavorites on tr.TrackId equals usr.TrackId into UsrTracks
from usrtr in UsrTracks.DefaultIfEmpty()
select new Track
{
IsFavorite = (usrfav.TrackId == null) ? false : true
}

关于c# - Linq select tablemapping( Entity Framework ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37527539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com