C# Linq any-如何选择

C# Linq any-如何选择,c#,.net,linq,entity-framework,C#,.net,Linq,Entity Framework,我有一些简单的类,如下所示: Class Favorites Guid UserId Guid ObjectId Class Objects Guid Id String Name 使用Entity Framework,我想选择用户标记为收藏夹的所有对象 所以我试过这样的方法 context.Objects.Where( x => x.Id == context.Favorite.Where(f => f.UserId == UserId) .Select(f

我有一些简单的类,如下所示:

Class Favorites
Guid UserId
Guid ObjectId

Class Objects
Guid Id
String Name
使用Entity Framework,我想选择用户标记为收藏夹的所有对象

所以我试过这样的方法

context.Objects.Where(
   x => x.Id == 
   context.Favorite.Where(f => f.UserId == UserId)
   .Select(f => f.ObjectId).Any()
);

但我不明白。我也尝试过intersect,但我最了解的是它的类型。一个用户可以有许多喜爱的对象

使用FirstOrDefault()而不是Any()

您可以使用join子句:

context.Favorite
  .Where(f => f.UserId == UserId)
  .Join(context.Objects, t => t.ObjectId, u => u.Id, (t, u) => t);

我会加入,我的linq看起来像:

var matches = from o in context.Objects
join f in context.Favorite on o.Id equals f.ObjectId
where f.UserId == UserId
select o;

您的favorites类需要一个将其链接回对象的属性,并且可以使用Include而不是double where子句

班级

Class Favorites
    Guid UserId
    Guid ObjectId
    [ForeignKey("Id")]
    Objects objs

Class Objects
    Guid Id
    String Name
林克


然后,您的收藏夹对象下会有一组对象。

您是否有导航属性,即Favorite.object?我更新了问题。一个用户有许多收藏夹。所以第一个或默认值只匹配第一个ahsant,ye+1 ham az tarafe ma
Class Favorites
    Guid UserId
    Guid ObjectId
    [ForeignKey("Id")]
    Objects objs

Class Objects
    Guid Id
    String Name
context.Favorites.Include(objs).Where(x => x.UserID == UserID)