0

I have this list of strings:

List<string> clubs = dropdownSelectedvalue.Split(',').ToList();

The values will be for example: 1255,1233,2233. Now I have object which has the same propertyid as the values in the clubs list. What I want to achieve is clear out the clubs list from the existing propertyid's in case the id is in the list and only leave the ids which are not existing.

This is the object:

public class Clubs
{
    public int? ClubId { get; set; }    
    public string ClubName { get; set; }
}

Is there any linq command or something similar which I can utilize for this issue?

Code added:

List<string> newClubs = clubs.Remove(s => Club.ClubId.ToString().Equals(clubs));

Something like this but it's not completed, I've reached that point.

Additional code added per request:

List<Clubs> Clubs = Clubs.LoadAll();
foreach (Clubs club in Clubs)
{
    var dropdownSelectedvalue = values[sponsor.WebUserId.Value.ToString()];
    List<string> clubs = dropdownSelectedvalue.Split(',').ToList();             
    List<string> newClubs = ?                
}
5
  • 1
    Is there any linq command or something similar which I can utilize for this issue? Yes there is. Any other question?
    – L.B
    Sep 4, 2014 at 21:29
  • The reason you are getting downvotes is we'd like to see what LINQ operators you've tried and where you've gotten stuck with your own implementation.
    – Kirk Woll
    Sep 4, 2014 at 21:33
  • 1
    @KirkWoll L.B I've added code sample of what I've tried (incomplete). Thanks
    – Laziale
    Sep 4, 2014 at 21:38
  • @Laziale, what is your source list that contains the existing clubs? Is it a variable? Is it a single club? Can you show that code?
    – Kirk Woll
    Sep 4, 2014 at 21:41
  • 1
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?. Sep 4, 2014 at 21:53

2 Answers 2

2

English: Give me all clubs WHERE my list of club IDs does not CONTAIN the club's ClubId

Linq:

var query = clubList.Where(c => !clubs.Contains(c.ClubID));
2
  • is it possible a new list of strings to be generated from the filtered list? query.ToLIst() is giving me empty list of strings. Thanks
    – Laziale
    Sep 4, 2014 at 21:50
  • @Laziale Not sure I follow - are you wanting a list of clubs or a list of strings? If you want a list of the club Names just add .Select(c=>c.ClubName) to the end.
    – D Stanley
    Sep 4, 2014 at 22:05
1

The most readable way would be:-

var newClubs = clubs.Except(allClubs).ToList();

and you would implemnt IEquatable<Club> on your clubs class.

See http://msdn.microsoft.com/en-us/library/vstudio/bb300779(v=vs.100).aspx

Or, if you want to deal only in strings, project your allClubs list to just the names of the clubs (using Select) and then use Except on the strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.