ElHaix
ElHaix

Reputation: 12986

How to get a list of matched ID's using Linq?

I am maintaining a dictionary of clients that are logged in with _clientList.Add(clientIDGuid, newsfeedClient);

NewsfeedClient class:

public class NewsfeedClient
{
    /// <summary>
    /// Represents Newsfeed's own clientID
    /// </summary>
    public string ClientIDGuid { get; set; }  // also adding the dictionary's ID

    /// <summary>
    /// Represents the logged in client's session ID
    /// </summary>
    public string SessionID { get; set; }
    public string Nickname { get; set; }
    public int ProfileID { get; set; }

    public GeoLocation Location { get; set; }

    public List<MyDB.Models.Buddy> BuddyList { get; set; }

    [ScriptIgnore]
    public DateTime LastResponseTime { get; set; }

    public class GeoLocation
    {
        public float Latitude { get; set; }
        public float Longitude { get; set; }
    }
}

The Buddy object contains a ProfileID property.

I want to get a list of all dictionary clientID's where the matching profileID is in the BuddyList of that new client.

I'm guessing that the Intersect method would come into play, as seen here, but I'm not quite sure how to put this together without creating at least one foreach loop.

-- UPDATE --

The clientID is not == ProfileID these values are different.

Upvotes: 1

Views: 2594

Answers (2)

Mathew Thompson
Mathew Thompson

Reputation: 56429

Try this, assuming I've read your question correctly:

var matches = _clientList
            .Where(c => c.Value.BuddyList.Any(b => b.ProfileID == c.Value.ProfileID))
            .Select(c => c.Key)
            .ToList();

Upvotes: 1

Tejs
Tejs

Reputation: 41236

Assuming, I have your question correct, it should be like so:

var matches = _clientList.ToList()
    .Select(x => x.Key)
    .Intersect(currentNewsFeed.BuddyList.Select(x => x.ProfileId))
    .ToList();

Upvotes: 1

Related Questions