我有实体程序。
Class Program
{
public int Id {get;set;}
public string ProgramName {get;set;}
public List<Activity> Activities {get;set}
}
活动看起来像:-
Class Activity
{
public int Id {get;set;}
public string Name {get;set;}
}
现在,我需要获取活动Id
=参数Id
的所有程序。
这就是我尝试的var programs = _context.Program.Where(p => p.Activities.Select(a => a.Id).Contains(Id)).ToList();
但它给出了类似的错误
"The LINQ expression 'DbSet<Programs>rn .Where(p => p.LocationId == __model_Tags_0 || p.Activityrn .Select(a => a.Id)rn .Contains(__model_Tags_0) && True)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
解决这个问题的更好方法(linq查询(是什么?
您可以在Where
条件中使用类似于p.Activities.Any(a => a.Id == Id)
的Any
,如下所示。
var programs = _context.Program.Where(p => p.Activities.Any(a => a.Id == Id)).ToList();