c# - Select lambda usage -
sorry perhaps noob question) trying learn new. have array holds object many fields - how check select if example first field of object equal string ? (this field string no type ops needed)
consider scenario:
// data object public class data { public string name { get; set; } public int value { get; set; } public data(string name, int value) { this.name = name; this.value = value; } } // array data[] array = new data[] { new data("john smith", 123), new data("jane smith", 456), new data("jess smith", 789), new data("josh smith", 012) } array.any(o => o.name.contains("smith")); // returns true if object's name property contains "smith"; otherwise, false. array.where(o => o.name.startswith("j")); // returns ienumerable<data> items in original collection name starts "j" array.first(o => o.name.endswith("smith")); // returns first data item name ends "smith" array.singleordefault(o => o.name == "john smith"); // returns single element name "john smith". // if number of elements name "john smith" // greater 1, throw exception. // if no elements found, this` return null. // (singleordefault intended selecting unique elements). array.select(o => new { fullname = o.name, age = o.value }); // projects data[] ienumerable<{fullname, value}> // {fullname, value} anonymous type, name projects fullname , value projects age.
Comments
Post a Comment