我有代码只使用一个属性"名称"构建列表.如何修改代码,以便它可以构建具有两个属性"Name"和"Test_Result"的列表我知道匿名类型可以用来执行此操作,但是如何将它们用于动态表达式?这是我的代码:
string item = "Name";
string item2 = "Test_Result";
Type studentType = typeof(Student);
ParameterExpression itemParam = Expression.Parameter(studentType, item);
MemberInfo itemProperty = studentType.GetProperty(item);
MemberExpression valueInItemField =
Expression.MakeMemberAccess(itemParam, itemProperty);
Expression<Func<Student, string>> selectExpression =
Expression<Func<Student, string>>
.Lambda<Func<Student, string>>(valueInItemField, itemParam);
IEnumerable<string> currentItemFields =
DeserializedStudents.Select(selectExpression.Compile());
Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个用于排序的表达式,并且我编写了使用一个属性对列表进行排序的代码.
但我需要首先将其分类为一个属性,其次是另一个属性,依此类推.
我的意思是我想构建一个表达式来实现类似的东西:students.OrderBy(fistExpression.Compile()).ThenBy(secondImpression.Complie()).ThenBy(thirdExpression.Compile()).
那么如何动态地放置那些ThenBy方法呢?
这是我的代码:
Type studentType = typeof(Student);
ParameterExpression studentParam = Expression.Parameter(studentType, "x");
MemberInfo ageProperty = studentType.GetProperty("Age");
MemberExpression valueInNameProperty =
Expression.MakeMemberAccess(studentParam, ageProperty);
Expression<Func<Student, int>> orderByExpression =
Expression<Func<Student, int>>.Lambda<Func<Student, int>>(valueInNameProperty, studentParam);
var sortedStudents = students.OrderBy(orderByExpression.Compile());
Run Code Online (Sandbox Code Playgroud) 我必须写一个正则表达式来从文本中获取三个单词.单词用一个空格分隔.我编写的代码并不是所有的序列.例如,对于文本"一二三四五六",我只得到两个序列:1.一个两个三个2.四个五个六个.但我希望我的正则表达式给我所有序列,所以输出将是:1.两个三个2.两个三个四个3.三个四个五个.四点六.请问有人告诉我,我的正则表达式有什么问题吗?这是我的代码:
string input = "one two three four five six";
string pattern = @"([a-zA-Z]+ ){2}[a-zA-Z]+";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
Console.WriteLine("{0} ({1} matches):", input, matches.Count);
Console.WriteLine();
foreach (Match match in matches)
Console.WriteLine(match.Value);
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)