Lambda表达式树 [英] Lambda Expression Tree
问题描述
我正在做一些改进项目。
我想修改lambda表达式树,以便我可以编译它。按照现在的逻辑:
假设我想要一些学生的数据,从第8天和第9天开始。所以当前的表达式树构建如下:
std == 8th std and student = student 1
std == 8th std and学生=学生2
std == 8th std and student = student 3.
std == 9th std and student = student 1
std == 9th std and student = student 2
std == 9th std and student = student 3.
因为这很耗时所以我想修改为:
std == 8th和学生(学生1,学生) 2,学生3)
std == 9th和学生(学生1,学生2,学生3)是这样的。
任何想法怎么做?
你的问题没有定论:你是否要求像 dynamix LINQ [ ^ ]?或者我的CP示例:发明您自己的动态LINQ解析器 [ ^ ] ?
它们都接受一个字符串并将其转换为一个编译的表达式树,可以用于查询等(例如用于LINQ查询)。
干杯
Andi
使用这种表达方式:
Student.Where( c => c.std ==8&& c => c.student ==student1);
是你的student1,student2,student3总是这3个值(或一些 3个特定值)?
或者是这样的情况你有一些集合的价值?
// 如果ID值总是相同:
private static readonly 列表< string> ID = new 列表< string>(){ student1 , student2, student3};
public static bool MatchingStudent ( StudentType 学生, int std)
{
// 添加对学生为空的检查!!
// 根据您的需要使用适当的StringComparer
return student.std == std&& IDs.Contains(student.StudentID,StringComparer.CurrentCultureIgnoreCase);
}
// 如果ID值在某个集合中:
public static bool MatchingStudent( StudentType 学生, int std,IEnumerable< string> studentIDs)
{
// 添加对student和studentID为空的检查!!
// 根据您的需要使用适当的StringComparer
return student.std == std& &安培; studentIDs.Contains(student.StudentID,StringComparer.CurrentCultureIgnoreCase);
}
然后用法为:
var matching = Student.Where(s = > MatchingStudent(s, 8 ) );
// 或者,视上述情况而定
var matching = Student.Where(s = > MatchingStudent(s, 8 , IDsCollection ));
如果你有std值可以的情况成为众多之一,然后你可以扩展上面的IEnumerable< int> stds
而不是int std
,并将student.std == std
更改为stds.Contains(student.std)
Hi,
I am doing some enhancement project.
I want to modify lambda expression tree so that I can compile it. In present logic :
Suppose I want data of some students from 8th std & 9th std. So the current expression tree is building like this:
std== 8th std and student= student 1
std== 8th std and student= student 2
std == 8th std and student = student 3.
std== 9th std and student= student 1
std== 9th std and student= student 2
std == 9th std and student = student 3.
As this is time consuming so I want to modify as:
std==8th and student in (student 1, student 2, student 3)
std==9th and student in (student 1, student 2, student 3) like this.
Any idea how to do this?
Your question is not conclusive: Do you ask for somethinng like dynamix LINQ[^]? Or an example of mine on CP: Invent your own Dynamic LINQ parser[^]?
They both take a string and convert it into an expression tree that is compiled and can be used for queries and alike (e.g. for LINQ queries).
Cheers
Andi
use this kind of expressions :
Student.Where(c => c.std == "8" && c=>c.student=="student1");
Are your "student1", "student2", "student3" always these 3 values (or some 3 specific values)?
Or is the case that you have some collection of values?
// if the ID values are always the same: private static readonly List<string> IDs = new List<string>(){ "student1", "student2", "student3" }; public static bool MatchingStudent(StudentType student, int std) { // add check for student being null!! // use the appropriate StringComparer based on your needs return student.std == std && IDs.Contains(student.StudentID, StringComparer.CurrentCultureIgnoreCase); } // if the ID values are in some collection: public static bool MatchingStudent(StudentType student, int std, IEnumerable<string> studentIDs) { // add checks for student and studentIDs being null!! // use the appropriate StringComparer based on your needs return student.std == std && studentIDs.Contains(student.StudentID, StringComparer.CurrentCultureIgnoreCase); }
Then the usage is:
var matching = Student.Where(s => MatchingStudent(s, 8)); // OR, depending on the cases above var matching = Student.Where(s => MatchingStudent(s, 8, IDsCollection));
If you have the cases where the "std" value can be one of many, then you could extend the above withIEnumerable<int> stds
instead ofint std
, and change thestudent.std == std
tostds.Contains(student.std)
.
这篇关于Lambda表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!