This Query returns left outer join from two tables.
var q = from adu in dtAdsUsers.AsEnumerable()
join elu in dtElUsers.AsEnumerable() on adu.Field<string>("samAccountName") equals elu.Field<string>("UserLogin") into gj
from au in gj.DefaultIfEmpty() //where au.Field<string>("UserLogin")==string.Empty
select
new
{
samAccount = adu.Field<string>("samAccountName"),
samb = (au == null ? string.Empty : au.Field<string>("UserLogin"))
};
Now remove entries from the dtAdusers which is bound to bindinglist bUserList, using code like this
foreach (var o in q)
{
if (o.samb != "")
{
Debug.WriteLine(o.samAccount);
IEnumerable<ADSUtilities.User> ud = from u in bUserList.AsEnumerable()
where u.sAMAccountName == o.samAccount
select u;
bUserList.RemoveAt(bUserList.IndexOf(ud));
}
}
bUserList.RemoveAt(bUserList.IndexOf(ud)) gives invalid cast exception
1). How to resolve this ?
2). is there a Linq way of removing entries without iterating ?
udis anIEnumerablewhile the elements inbUserListare presumably not ofIEnumerablethis is whybUserList.IndexOf(ud)will fail.If the query for
udcan indeed result in multiple objects then you need to remove them 1 by 1 like thisIf the query for
udis supposed to return only one element then you should change it to:Then you can do:
bUserList.Remove(ud);Use
FirstOrDefaultif the query can result in no objects. This will returndefault(T)(which isnullfor reference types) instead of throwing an exception.There is no real “Linq way” of removing elements from a list because you can’t enumerate a list and remove items from it at the same time. This would result in an exception. So you always need to identify the items you want to delete first, store them in a separate list and then remove them. You could make an extension method for if you require it more often. Something like this: