Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinqKit applying nested filtering through extension-method not working #139

Open
kuebelkasten opened this issue Mar 15, 2021 · 12 comments
Open

Comments

@kuebelkasten
Copy link

kuebelkasten commented Mar 15, 2021

See stackoverflow fo details.

Seems like the Get is never called (breakpoints never hit). I already tried using Expression.Lambda<> Compile(), Invoke() and Expand() but still, the extension seems not be invoked before the exception is thrown.

On the other hand, using Where works as expected.

@sdanyliv
Copy link
Contributor

sdanyliv commented Mar 15, 2021

@kuebelkasten, it is your question in SO?

@kuebelkasten
Copy link
Author

@sdanyliv Yes. Was not sure if I need to repeat it completely here again. But would be possible to edit my issue here if needed

@sdanyliv
Copy link
Contributor

Well, answer added: https://stackoverflow.com/a/66650693/10646316

@kuebelkasten
Copy link
Author

kuebelkasten commented Mar 16, 2021

@sdanyliv I've added the Expandable-attribute to the Get extension-method ending up in exception

[Expandable]
public static IEnumerable<TEntity> Get<TEntity, TEntityFilter>(this IEnumerable<TEntity> entities, TEntityFilter filter)
    where TEntity : BaseEntity<TEntity>, new()
    where TEntityFilter : EntityFilter<TEntity>
{
    return entities.Where(e => filter.FilterExpression.Invoke(e));
}

No generic method 'Get' on type 'Project.Database.Extensions.DbSetExtensions' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

Could it be possbile that the issue is because there are two methods named Get?

@sdanyliv
Copy link
Contributor

It is not correct way of using Expandable attribute and is not original SO question.

@kuebelkasten
Copy link
Author

kuebelkasten commented Mar 16, 2021

Sorry. I copied the wrong link to the SO-question. Should be stackoverflow I updated the question accordingly. No idea where the wrong link was from...

@sdanyliv
Copy link
Contributor

Done.

@kuebelkasten
Copy link
Author

@sdanyliv Awesome. Well appreciated. In your copied code there is just one > at the wrong position in GetEnumerable but then it works like a charm. Many thanks 👍

@sdanyliv
Copy link
Contributor

Anyway, try to remove Expand, probably it may work also.

@kuebelkasten
Copy link
Author

Yupp. Removed Expand() in both methods and it still works as expected

@kuebelkasten
Copy link
Author

kuebelkasten commented Mar 17, 2021

Maybe @sdanyliv could help me out with this again as it may be related. I have the following query:

await DbContext.Events.Get(new EventFilter
{
	IsVisibleFilter = (EventIsVisibleFilter)true,
	IdIncludeExcludeFilter = (EventIdIncludeExcludeFilter)new IncludeExcludeFilter<Guid> { Includes = new[] { eventId } }
}).Select(e => new
{
	EventTeams = e.EventTeams
		.Get(new EventTeamFilter { IsVisibleFilter = (EventTeamIsVisibleFilter)true })
		.Select(et => new
		{
			et.EventTeamTeam.TeamId,
			et.EventTeamTeam.TeamName,
			Players = et.EventTeamDivision.EventPlayers
				.Get(new EventPlayerFilter
				{
					IsVisibleFilter = (EventPlayerIsVisibleFilter)true,
					TeamFilter = new TblTeamFilter { IdIncludeExcludeFilter = (TeamIdIncludeExcludeFilter)new IncludeExcludeFilter<Guid> { Includes = new[] { et.EventTeamTeamId } } } // here is the exception
				})
				//.Where(tit => tit.EventPlayerTeamId == et.EventTeamTeamId) // this works
				.Select(pit => new
				{
					pit.EventPlayerPlayer.PlayerId,
					pit.EventPlayerPlayer.PlayerFirstName,
					pit.EventPlayerPlayer.PlayerLastName,
				})
		})
}).SingleAsync()

If I run this query I get the exception

System.InvalidOperationException: variable 'et' of type 'Project.Database.Entities.EventTeam' referenced from scope '', but it is not defined

When changing the second Get-call to Where:

.Where(pit => new EventPlayerFilter
{
	IsVisibleFilter = (EventPlayerIsVisibleFilter)true,
	TeamFilter = new TblTeamFilter { IdIncludeExcludeFilter = (TeamIdIncludeExcludeFilter)new IncludeExcludeFilter<Guid> { Includes = new[] { et.EventTeamTeamId } } }
}.FilterExpression.Compile().Invoke(pit))

the exception

.FilterExpression.Compile().Invoke(t4))' 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.

@kuebelkasten kuebelkasten reopened this Mar 17, 2021
@sdanyliv
Copy link
Contributor

I need full call stack. Anyway, weird filtering technique.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants