c# - Group IEnumerable into a string -
i wonder if spare me few minutes give me advice please?
i've created ienumerable
list:
public class emailblock { public int alertcategory { get; set; } public string alertname { get; set; } public string alerturl { get; set; } public string alertsnippet { get; set; } //need work out snippet } list<emailblock> myemaildata = new list<emailblock>();
which loop through data (umbraco content - not that's relevant!) , add items list.
myemaildata.add(new emailblock { alertcategory = category.id, alertname = alert.getpropertyvalue("pagetitle"), alerturl = alert.niceurl });
what i'd group list alertcategory
, load each 'group' (another loop occurs later check members have subscribed alert category) variable can use email's content.
linq has nice group statement:
var emailgroup = emaillist.groupby(e => e.alertcategory);
then can loop through each grouping , whatever want:
foreach(var grouping in emailgroup) { //do whatever want here. //note grouping access list of grouped items, grouping.key show grouped field }
edit:
to retrieve group after have grouped them, use where
more 1 or first
one:
var group = emailgroup.first(g => g.key == "name looking for");
or
var groups = emailgroup.where(g => listofwantedkeys.contains(g.key));
this lot more efficient looping through every time need find something.
Comments
Post a Comment