Hien Pham Minh
Hien Pham Minh

Reputation: 1

How i can pass multi property of class as parameter of method using C#

I have a class that will be inherit in onother class. The content of class:

public T First(Expression<Func<T, string>> OrderBy = null)
{
   string orderby=string.Empty;
   if (OrderBy != null)
   {
      System.Reflection.PropertyInfo orderbyinfo= (System.Reflection.PropertyInfo)  ((MemberExpression)OrderBy.Body).Member;
            orderby = orderbyinfo.Name;
   }
   ...

}

But error "Can not convert lambda expression to delegate type ..." with method:

public T First(Expression<Func<T, string>> OrderBy = null,Expression<Func<T, string>> GroupBy = null)
{
   string orderby=string.Empty;
   string groupby=string.empty
   if (OrderBy != null)
   {
      System.Reflection.PropertyInfo orderbyinfo= (System.Reflection.PropertyInfo)   ((MemberExpression)OrderBy.Body).Member;
            orderby = orderbyinfo.Name;
   }

   if (GroupBy != null)
   {
      System.Reflection.PropertyInfo groupbyinfo= (System.Reflection.PropertyInfo)  ((MemberExpression)GroupBy.Body).Member;
            groupby = groupbyinfo.Name;
   }
   ...

}

And method as:

Article article = new Article();
article.Title="test";
article=article.First(x=>x.Title,y=>y.Status);

Please help me! thanks!!!

Upvotes: 0

Views: 266

Answers (1)

t3chb0t
t3chb0t

Reputation: 18655

You get this error because Status probably isn't a string and your Funcs can return only a string:

public T First(
    Expression<Func<T, string>> orderBy = null,
    Expression<Func<T, string>> groupBy = null)

if you make them generic you'll be able to return anything. Because both properties can be of different types you'll need two generic types (TProperty1 TProperty2), one for each of them:

public T First<TProperty1, TProperty2>(
    Expression<Func<T, TProperty1>> orderBy = null, 
    Expression<Func<T, TProperty2>> groupBy = null)

Example:

class C<T>
{        

    public T First<TProperty1, TProperty2>(
        Expression<Func<T, TProperty1>> orderBy = null, 
        Expression<Func<T, TProperty2>> groupBy = null)
    {
        string orderByName = string.Empty;
        string groupByName = string.Empty;
        if (orderBy != null)
        {
            System.Reflection.PropertyInfo orderByInfo = (System.Reflection.PropertyInfo)((MemberExpression)orderBy.Body).Member;
            orderByName = orderByInfo.Name;
        }

        if (groupBy != null)
        {
            System.Reflection.PropertyInfo groupByInfo = (System.Reflection.PropertyInfo)((MemberExpression)groupBy.Body).Member;
            groupByName = groupByInfo.Name;
        }
        ....
    }
}

Usage: here both properties are ints

new C<string>().First(x => x.Length, x => x.Length);

However if they must be strings then you need to convert/cast the values to strings:

article = article.First(x=>x.Title, y=>y.Status.ToString());

Would you have read all error messages and posted all of them here you would have noticed that there is a line that tells you about the conversion error:

Error 1 Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type

Error 2 Cannot implicitly convert type 'int' to 'string'

Upvotes: 1

Related Questions