状況は次のとおりです。Web ページは、ログインしているユーザーにギャザリングのリストを表示します。このクエリ可能なグループの中には、ユーザーが購読しているが、このページをロードするまでまだ表示されていないギャザリングが含まれる場合があります。ギャザリングがこのユーザーによって表示されたので、そのようにマークしたいと思います。
ToList() と Contains() (以下を参照) を使用してこれを機能させることができますが、これはデータベースに 2 回アクセスする必要があることを意味します。これらのサブスクリプションを取得する他の方法を試しましたが、代わりに EntitySet になります。
Users Gatherings
===== ==========
UserId GatheringId
GatheringSubscriptions
======================
GatheringSubscriptionId
GatheringId
UserId
IsViewed
// update unviewed=>viewed for this user's subscription to each
// of these gatherings
public void MarkViewed(IQueryable<Gathering> gatherings, int userId) {
List<int> gIdsList = gatherings.Select(g => g.GatheringId).ToList();
IQueryable<GatheringSubscription> gSubs = db.GatheringSubscriptions
.Where(gs =>
gs.UserId == userId &&
!gs.IsViewed &&
gIdsList.Contains(gs.GatheringId))
.Distinct();
foreach (GatheringSubscription gSub in gSubs)
gSub.IsViewed = true;
db.SubmitChanges();
}
どうすれば同じことを実現できますか?データベースへのトリップは 1 回だけです。