Thursday, 15 September 2011

c# - Nested Lambda Expressions -



c# - Nested Lambda Expressions -

does have thought how phone call lambda look within lambda expression?

if have:

public class courseviewmodel { public int id { get; set; } public string name { get; set; } public static expression<func<course, courseviewmodel>> asviewmodel = x => new courseviewmodel { id = x.id, name = x.name, } } public class studentviewmodel { public int id { get; set; } public string name{ get; set; } public string preferredcheese { get; set; } public ienumerable<courseviewmodel> courses { get; set; } public static expression<func<student, studentviewmodel>> asviewmodel = x => new studentviewmodel { id = x.id, name = x.name, preferredcheese = x.preferredcheese, courses = ???i'd phone call courseviewmodel.asviewmodel here } }

in code above rather writing asviewmodel look within studentviewmodel as:

courses = new courseviewmodel { id = x.id, name = x.name, }

i'd phone call courseviewmodel.asviewmodel allow code re-use , maintain code converting course of study courseviewmodel in courseviewmodel class. possible?

you can utilize x.courses.select(c => courseviewmodel.asviewmodel(c)) whole look be:

public static expression<func<student, studentviewmodel>> asviewmodel = x => new studentviewmodel { id = x.id, name = x.name, preferredcheese = x.preferredcheese, courses = x.courses.select(c => courseviewmodel.asviewmodel(c)) }

c# lambda expression-trees

No comments:

Post a Comment