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

System.ArgumentException : Argument types do not match if right part is LINQ #133

Closed
EvgenyMarchuk opened this issue Oct 20, 2023 · 2 comments
Assignees
Labels

Comments

@EvgenyMarchuk
Copy link

EvgenyMarchuk commented Oct 20, 2023

Hello! It's me again. Found a new defect with the same problem.
Sorry for the spam, added comment to the previose bug last week
but decided to open a new defect.

.ResponseValue(x => x.Any(c => c.Currency == source.Currency.First().Code));
Unhandled exception. System.ArgumentException: Argument types do not match
   at System.Linq.Expressions.Expression.Constant(Object value, Type type)
   at AgileObjects.ReadableExpressions.Translations.MemberAccessTranslation.For(MemberExpression memberAccess, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetDefaultTranslation(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslationFor(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.BinaryTranslation..ctor(BinaryExpression binary, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.BinaryTranslation.For(BinaryExpression binary, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetDefaultTranslation(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslationFor(Expression expression)
   at AgileObjects.ReadableExpressions.Extensions.PublicTranslationContextExtensions.GetCodeBlockTranslationFor(ITranslationContext context, Expression expression)
   at AgileObjects.ReadableExpressions.Translations.LambdaTranslation..ctor(LambdaExpression lambda, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetDefaultTranslation(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslationFor(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ParameterSetTranslation.GetParameterTranslation(Expression parameter, IParameter info, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ParameterSetTranslation.<>c__DisplayClass7_0.<.ctor>b__0(Expression p, Int32 index)
   at AgileObjects.ReadableExpressions.Extensions.InternalEnumerableExtensions.Project[TItem,TResult](IEnumerable`1 items, Func`3 projector)+MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at AgileObjects.ReadableExpressions.Translations.ParameterSetTranslation..ctor(IMethodBase method, IEnumerable`1 parameters, Boolean showParameterTypeNames, Int32 count, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ParameterSetTranslation.For[TParameterExpression](IMethodBase method, ICollection`1 parameters, Boolean showParameterTypeNames, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ParameterSetTranslation.For[TParameterExpression](IMethod method, ICollection`1 parameters, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.MethodCallTranslation.For(MethodCallExpression methodCall, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetDefaultTranslation(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslationFor(Expression expression)
   at AgileObjects.ReadableExpressions.Extensions.PublicTranslationContextExtensions.GetCodeBlockTranslationFor(ITranslationContext context, Expression expression)
   at AgileObjects.ReadableExpressions.Translations.LambdaTranslation..ctor(LambdaExpression lambda, ITranslationContext context)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetDefaultTranslation(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslationFor(Expression expression)
   at AgileObjects.ReadableExpressions.Translations.ExpressionTranslation.GetTranslation()
   at AgileObjects.ReadableExpressions.ExpressionExtensions.ToReadableString(Expression expression, Func`2 configuration)
   at ConsoleApp2.LambdaExpressionExtension.GetMember(LambdaExpression memberSelector) in C:\Users\EV\source\repos\ConsoleApp2\Program.cs:line 97
   at ConsoleApp2.ResponseValidator`1.ResponseValue[TProperty](Expression`1 property) in C:\Users\EV\source\repos\ConsoleApp2\Program.cs:line 60
   at ConsoleApp2.Program.Main(String[] args) in C:\Users\EV\source\repos\ConsoleApp2\Program.cs:line 40
namespace ConsoleApp2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var source = new BalanceSource
            {
                Currency = new List<ExcludedCurrency>
                {
                    new()
                    {
                        Code = CurrencyType.USD
                    }
                }
            };

            var balances = new List<BalanceModel>
                {
                    new()
                    {
                        Amount = 1,
                        Currency = CurrencyType.EUR
                    },
                    new()
                    {
                        Amount = 5,
                        Currency = CurrencyType.USD
                    },
                    new()
                    {
                        Amount = 10,
                        Currency = CurrencyType.GBP
                    }
            };

            new ResponseValidator<List<BalanceModel>>(balances)
                .ResponseValue(x => x.Any(c => c.Currency == source.Currency.First().Code));
        }
    }

    public class ResponseValidator<T>
    {
        private readonly T _response;

        public ResponseValidator(T response)
        {
            _response = response;
        }

        public ResponseValidator<T> ResponseValue<TProperty>(Expression<Func<T, TProperty>> property)
        {
            var member = property.GetMember();
            var compiledProperty = property.Compile()(_response);

            Console.WriteLine($"{member}: {compiledProperty}");
            return this;
        }
    }

    public class BalanceModel
    {
        public double Amount { get; set; }
        public CurrencyType Currency { get; set; }
    }

    public class BalanceSource
    {
        public List<ExcludedCurrency> Currency { get; set; }
    }

    public class ExcludedCurrency
    {
        public CurrencyType Code { get; set; }
    }

    public enum CurrencyType
    {
        EUR,
        GBP,
        USD
    }

    public static class LambdaExpressionExtension
    {
        public static string GetMember(this LambdaExpression memberSelector)
        {
            var i = new Func<ITranslationSettings, ITranslationSettings>(s => s.ShowCapturedValues);

            return memberSelector.ToReadableString(i);
        }
    }
}
@SteveWilkes
Copy link
Member

Not that it's been a really long time or anything, but this is fixed in the vNext release branch. Release to follow!

@SteveWilkes SteveWilkes added the in-branch A feature or bug fix exists in code, and a release will follow label Jun 22, 2024
@SteveWilkes
Copy link
Member

This is fixed as of v4.1.2, which is now available on NuGet. Cheers!

@SteveWilkes SteveWilkes removed the in-branch A feature or bug fix exists in code, and a release will follow label Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants