Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:47:05+00:00 2026-05-26T18:47:05+00:00

This Query returns left outer join from two tables. var q = from adu

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:47:05+00:00Added an answer on May 26, 2026 at 6:47 pm

    ud is an IEnumerable while the elements in bUserList are presumably not of IEnumerable this is why bUserList.IndexOf(ud) will fail.

    If the query for ud can indeed result in multiple objects then you need to remove them 1 by 1 like this

    foreach(var doomed in ud)
        bUserList.Remove(doomed);
    

    If the query for ud is supposed to return only one element then you should change it to:

     ADSUtilities.User ud = bUserList.AsEnumerable().First(u => u.sAMAccountName == o.samAccount);
    

    Then you can do: bUserList.Remove(ud);

    Use FirstOrDefault if the query can result in no objects. This will return default(T) (which is null for 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:

     public static RemoveIf<T>(this IList<T> list, Predicate<T> cond)
     {
         var doomed = list.Where(cond).ToList();
         doomed.ForEach(x => list.Remove(x));
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This query works great: var pageObject = (from op in db.ObjectPermissions join pg in
I'm stuck on translating a left outer join from LINQToSQL that returns unique parent
Can anyone explain why this query returns an empty result. SELECT * FROM (`bookmarks`)
I have a LINQ query that returns some object like this... var query =
So I have this query: select ens_use_new_models_bit from cfo_transaction inner join dbo.cfo_trans_entity_rel on te_tr_transaction_id=tr_transaction_id
I'd like to make an (MS)SQL query that returns something like this: Col1 Col2
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
I have this query in MySQL: SELECT pr.*, pr7.value AS `room_price_high` FROM `jos_hp_properties` pr
This query works: SELECT Article.id, Article.post_time, Article.post_locked, Article.comments_locked, Article.title, IF(CHAR_LENGTH(Article.content)>2000, RPAD(LEFT(Article.content,2000),2003,'.'), Article.content) as content,
I have the following (simplified) query select P.peopleID, P.peopleName, ED.DataNumber from peopleTable P left

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.