C
C#6mo ago
stepa

need help with ef

var recordsToSkip = (_filmsPageIndex - 1) * 10;
IQueryable<Film> query = _context.Films
.Include(u => u.Janr)
.Include(u => u.Zal)
.Include(u => u.Actors)
.OrderByDescending(x => x.Id);
var recordsToSkip = (_filmsPageIndex - 1) * 10;
IQueryable<Film> query = _context.Films
.Include(u => u.Janr)
.Include(u => u.Zal)
.Include(u => u.Actors)
.OrderByDescending(x => x.Id);
if (FilmActorText.Length != 0)
{
query = query.Where(x => x.Actors != null && x.Actors.Any(actor => EF.Functions.Like(actor.Name, $"{FilmActorText}%")));
}
if (FilmActorText.Length != 0)
{
query = query.Where(x => x.Actors != null && x.Actors.Any(actor => EF.Functions.Like(actor.Name, $"{FilmActorText}%")));
}
_films = query.Skip(recordsToSkip)
.Take(10)
.ToList();
_films = query.Skip(recordsToSkip)
.Take(10)
.ToList();
public class Film
{
[Key]
public int Id { get; set; }
...
public List<Actor>? Actors { get; set; }
}
public class Film
{
[Key]
public int Id { get; set; }
...
public List<Actor>? Actors { get; set; }
}
public class Actor
{
[Key]
public int Id { get; set; }
public required string Name { get; set; }
[Browsable(false)]
public List<Film>? Films { get; set; }
}
public class Actor
{
[Key]
public int Id { get; set; }
public required string Name { get; set; }
[Browsable(false)]
public List<Film>? Films { get; set; }
}
There is exception
I’ll send the exception text in 5 minutes, it doesn’t fit into the limit
I’ll send the exception text in 5 minutes, it doesn’t fit into the limit
How do I fix it?
2 Replies
stepa
stepa6mo ago
System.InvalidOperationException: The LINQ expression 'DbSet<Film>()
.OrderByDescending(f => f.Id)
.Where(f => MaterializeCollectionNavigation(
Navigation: Film.Actors,
subquery: DbSet<Dictionary<string, object>>("ActorFilm")
.Where(a => EF.Property<int?>(f, "Id") != null && object.Equals(
objA: (object)EF.Property<int?>(f, "Id"),
objB: (object)EF.Property<int?>(a, "FilmsId")))
.Join(
inner: DbSet<Actor>(),
outerKeySelector: a => (object)EF.Property<int?>(a, "ActorsId"),
innerKeySelector: a0 => (object)EF.Property<int?>(a0, "Id"),
resultSelector: (a, a0) => new TransparentIdentifier<Dictionary<string, object>, Actor>(
Outer = a,
Inner = a0
))
.Select(ti => ti.Inner)) != null && DbSet<Dictionary<string, object>>("ActorFilm")
.Where(a1 => EF.Property<int?>(f, "Id") != null && object.Equals(
objA: (object)EF.Property<int?>(f, "Id"),
objB: (object)EF.Property<int?>(a1, "FilmsId")))
.Join(
inner: DbSet<Actor>(),
outerKeySelector: a1 => (object)EF.Property<int?>(a1, "ActorsId"),
innerKeySelector: a2 => (object)EF.Property<int?>(a2, "Id"),
resultSelector: (a1, a2) => new TransparentIdentifier<Dictionary<string, object>, Actor>(
Outer = a1,
Inner = a2
))
.Any(ti0 => __Functions_0
.Like(
matchExpression: ti0.Inner.Name,
pattern: __Format_1)))' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
System.InvalidOperationException: The LINQ expression 'DbSet<Film>()
.OrderByDescending(f => f.Id)
.Where(f => MaterializeCollectionNavigation(
Navigation: Film.Actors,
subquery: DbSet<Dictionary<string, object>>("ActorFilm")
.Where(a => EF.Property<int?>(f, "Id") != null && object.Equals(
objA: (object)EF.Property<int?>(f, "Id"),
objB: (object)EF.Property<int?>(a, "FilmsId")))
.Join(
inner: DbSet<Actor>(),
outerKeySelector: a => (object)EF.Property<int?>(a, "ActorsId"),
innerKeySelector: a0 => (object)EF.Property<int?>(a0, "Id"),
resultSelector: (a, a0) => new TransparentIdentifier<Dictionary<string, object>, Actor>(
Outer = a,
Inner = a0
))
.Select(ti => ti.Inner)) != null && DbSet<Dictionary<string, object>>("ActorFilm")
.Where(a1 => EF.Property<int?>(f, "Id") != null && object.Equals(
objA: (object)EF.Property<int?>(f, "Id"),
objB: (object)EF.Property<int?>(a1, "FilmsId")))
.Join(
inner: DbSet<Actor>(),
outerKeySelector: a1 => (object)EF.Property<int?>(a1, "ActorsId"),
innerKeySelector: a2 => (object)EF.Property<int?>(a2, "Id"),
resultSelector: (a1, a2) => new TransparentIdentifier<Dictionary<string, object>, Actor>(
Outer = a1,
Inner = a2
))
.Any(ti0 => __Functions_0
.Like(
matchExpression: ti0.Inner.Name,
pattern: __Format_1)))' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
I need to display movies that star one specific actor
VLADLEN
VLADLEN6mo ago
I would suggest to select actors that much ur condition first
var actorIds = await _context.Actors.Where(actor => EF.Functions.Like(actor.Name, $"{FilmActorText}%"))
.Select(x => x.Id)
.ToArrayAsync();
var actorIds = await _context.Actors.Where(actor => EF.Functions.Like(actor.Name, $"{FilmActorText}%"))
.Select(x => x.Id)
.ToArrayAsync();
Then select movies
var result = await _context.Films
.Include(u => u.Janr)
.Include(u => u.Zal)
.Include(u => u.Actors)
.Where(x => x.Actors != null && x.Actors.Any(act => actorIds.Contains(act.Id)))
.OrderByDescending(x => x.Id)
.Skip(recordsToSkip)
.Take(_recordsToTake)
.ToArrayAsync();

var result = await _context.Films
.Include(u => u.Janr)
.Include(u => u.Zal)
.Include(u => u.Actors)
.Where(x => x.Actors != null && x.Actors.Any(act => actorIds.Contains(act.Id)))
.OrderByDescending(x => x.Id)
.Skip(recordsToSkip)
.Take(_recordsToTake)
.ToArrayAsync();

This should work U can also do the same in one request by filtering actors table and joining Movies but my solution should be fine