dotnetnoob
dotnetnoob

Reputation: 11330

Linq query to select items in list from another list

I have a dictionary and a list.

AllMeta: is a dictionary<string, WikiMeta>

Meta: is a list<WikiMeta>

public class WikiMeta
{
    public string ContentTitle { get; set; }
    public string PageTitle { get; set; }
    public string PageMetaDescription { get; set; }
    public List<WikiArticle> Articles = new List<WikiArticle>();
    public List<WikiGroup> Groups = new List<WikiGroup>();
}

public class WikiGroup
{
    public string Name { get; set; }
}

I need to select values from AllMeta where AllMeta.Values.Group == Meta.Group returning a list of WikiMeta.

However, I'm struggling to construct the syntax (lambda), so any help would be appreciated.

Upvotes: 0

Views: 829

Answers (1)

Manish Mishra
Manish Mishra

Reputation: 12375

try this:

List<WikiMeta> wikis= AllMeta.Values
                             .Where(allmeta => 
                              Meta.Any(meta=> meta.Group == allmeta.Group))
                             .ToList();

where I've assumed following:

public class WikiMeta
{
   public string Name { get; set; }
   public string Group { get; set; }
}

public IDictionary<string,WikiMeta> AllMeta { get; set; }
public List<WikiMeta> Meta { get; set; }

make sure you referenced System.Linq namespace i.e.

using System.Linq;

Upvotes: 3

Related Questions