I have two IEnumerable objects, if a specific condition is met, i need to take each element from first IEnumerable to second IEnumerable. My code is like this
IEnumerable<IPublishedContent> nodesTemp = posts.Take(10).ToList();
IEnumerable<IPublishedContent> nodes = null;
foreach (var n in nodesTemp)
{
if (condition=true)
{
nodes.add(n);
}
}
But this throws an error
: 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'add' and no extension method 'add' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' could be found (are you missing a using directive or an assembly reference?)
Complete code for reference
IEnumerable<IPublishedContent> nodesTemp = posts.Take(count).ToList();
IEnumerable<IPublishedContent> nodes = null;
foreach (IPublishedContent n in nodesTemp)
{
var rolename = n.GetProperty("focusedUserGroup").Value.ToString();
var username = umbraco.cms.businesslogic.member.Member.GetCurrentMember().Text;
var flag = false;
if (!string.IsNullOrEmpty(rolename))
{
var groups = rolename.Split(',');
foreach (var group in groups)
{
if (Roles.IsUserInRole(username, group))
{
nodes.add(n);
break;
}
}
}
}
IEnumerableit's read only. take aList- Divyang DesaiFunc<IPublishedContent, bool>- StuartLCnodesasvar nodes = new List<IPublishedContent>();instead of= nullbefore the loop, you can.Add(capital A) to thatList<>. Such aList<>is implementsIEnumerable<>(and the latter interface is even covariant). - Jeppe Stig Nielsen