c# - Can I check the IEnumerable<T> before using yield return -
i have creating ienumerable stream creating each record 1 one. there way check current results if id exists don't want add read record set.
public class employee { public string name { get; set; } public string country { get; set; } public string rank { get; set; } public string hours { get; set; } public static employee create(idatarecord record) { return new employee { name = record["name"], country = record["country"], rank = record["rank"], hours = record["hours"], }; } } public ienumerable<employee> getemployees(textreader stream) { using (var reader = mylibraryfunction(stream) { while (reader.read()) { yield return employee.create(reader); } } } var result = getemployees(stream);
edit: simplified example. want know if can access existing records in set. if exists, update 1 field (add number of hours) in employee, if not exist should added.
use hashset of yielded object id`s :
public ienumerable<employee> getemployees(textreader stream) { var exisiting = new hashset<int>(); using (var reader = mylibraryfunction(stream) { while (reader.read()) { var employee = employee.create(reader); if (exisiting.add(employee.id)) { yield return employee; } } } }
Comments
Post a Comment