[英]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. 我有一个将在onother类中继承的类。 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: 但是出现错误“无法将lambda表达式转换为委托类型...”的方法:
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!!!
谢谢!!!
You get this error because Status
probably isn't a string
and your Func
s can return only a string
: 您会收到此错误,因为
Status
可能不是string
并且Func
只能返回一个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: 因为这两个属性可以属于不同的类型,所以您将需要两种通用类型(
TProperty1
TProperty2
),每种类型都需要一个:
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 int
s 用法:此处两个属性均为
int
new C<string>().First(x => x.Length, x => x.Length);
However if they must be string
s then you need to convert/cast the values to string
s: 但是,如果它们必须是
string
s,那么您需要将值转换/转换为string
s:
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
错误1无法将lambda表达式转换为委托类型'System.Func',因为该块中的某些返回类型不能隐式转换为委托返回类型
Error 2 Cannot implicitly convert type 'int' to 'string'
错误2无法将类型'int'隐式转换为'string'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.