2

I'm creating a LINQ extension using LINQKIT, but I can't figure out why I get this error

System.AggregateException: One or more errors occurred (no method 'InGroupsImpl on type 'SLT.Assets.Extensions.LINQExtensions' is compatible with the supplied arguments.)

The number of arguments on the implementing method matches the number of arguments from the extension, as far as I can see.

I can't understand what I'm missing. The extension exposes 3 parameters and the expression method takes 3 values.

Here's my code:

public interface IDbGroups
{
    [Required]
    string Groups { get; set; }
}

public enum MatchTypes
{
    None,
    Match,
    All
}

public static MatchTypes ListMatches(string list, string values)
{
    ...
}

[Expandable(nameof(Predicates.InGroupsImpl))]
public static bool DbInGroups(this IDbGroups item, string values)
    => throw new NotSupportedException();
    
public static Expression<Func<IDbGroups, string, bool>> InGroupsImpl()
    => (item, values) => DbUtility.ListMatches(item.Groups, values) != MatchTypes.None;

Usage:

cache.Settings = await db.GetWorker<Setting>().WhereAsync(s => s.AppId.Equals(cache.App.Id) || s.AppId == null && s.DbInGroups(Setting.DefaultGroupName).Equals(MatchTypes.All));

Type Setting implements IDbGorups

8
  • What are IDbGroups, DBUtility? You need to provide MCVE Commented Aug 9 at 7:35
  • @MichałTurczyn I've added more code. Basically I'm creating an exstension method on interface IDbGroups which has one property named Groups. I then providee a separated list of group nsmrd and gets an indicator on if any or all values exists in the Groups parameter. But the real issue is that I can't figure out why the arguments doesn't match with the expression method Commented Aug 9 at 8:05
  • Can you show how do you use that extension method, on which classes and LINQ Query? Also note that EF will not translate ListMatches. Commented Aug 9 at 8:20
  • @SvyatoslavDanyliv I've added an example on how I use the extension method. Basically I'm trying to create something like IN in sql, it returns true if supplied values exists in Groups, which is a comma separated list of group names. I can easily do it after locally after a query, but it would be better if it didn't return the row if the method returns False Commented Aug 9 at 8:31
  • 1
    Do not understand question. Translation is done only by EF Core. Expandable only replaces repetitive code fragments with simple functions. Commented Aug 9 at 10:20

1 Answer 1

3

As i found out this does not work if you use method from another class and refer to it with

[Expandable(nameof(Predicates.InGroupsImpl))]

It effectively resolves to InGroupsImpl, which is quite ambigous :)

I tried specifying

[Expandable("Predicates.InGroupsImpl")]

but that did not work.

What has worked is was moving InGroupsImpl to the same class as DbInGroups, and adjust the attribute as below:

public static class Extensions
{
    [Expandable(nameof(InGroupsImpl))]
    public static bool DbInGroups(this IDbGroups item, string values)
        => throw new NotImplementedException();

    public static Expression<Func<IDbGroups, string, bool>> InGroupsImpl()
        => (item, values) => DbUtility.ListMatches(item.Groups, values) != MatchTypes.None;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.