Matrix001
Matrix001

Reputation: 1272

I need help with using linq

I want to filter the objects that I have by their topic.

I have many topics: Arts, Economics, Business, Politics. Each topic is a property within the object that I try to classify from a list of those objects.

Here is part of my objects:

public class AllQuestionsPresented
{
    public string Name{ get; set; }
    public string ThreadName { get; set; }
    public string Topic { get; set; }
    public string Subtopic { get; set; }
    public int Views { get; set; }
    public int Replies { get; set; }
    public int PageNumber { get; set; }
    public DateTime Time { get; set; }
    // snip

I created many of those objects feed their properties with different values and put them into a List:

List<AllQuestionsPresented> forumData;

Now I want to group them all into linq by their topics..

var groupedByPages =
    from n in forumData
    group n by forumData
    select .....

Basically i dont know how to continue cause i am not used to deal with linq.. what i want to get is some dictionary..

Dictionary<string,AllQuestionsPresented> dictionary..

If i dont use linq, and add to a dictionary every topic, it will put several "AllQuestionsPresented" objects with the same topic..which will throw an exception..so i have to use group by..but dont know how to achieve that manipulation

Upvotes: 2

Views: 110

Answers (5)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234354

You can just call ToDictionary. The parameters are a function to select the keys and another to select the values:

var groupedByPages =
    from n in forumData
    group n by n.Topic;

IDictionary<string, IEnumerable<AllQuestionsPresented>> dictionary = 
    groupedByPages.ToDictionary(x => x.Key, x => x.AsEnumerable());

But if all you need from the IDictionary interface is the indexing operation, it's easier to just use a ILookup:

ILookup<string, AllQuestionsPresented> groupedByPages = forumData.ToLookup(x => x.Topic);

Upvotes: 0

Chris Taylor
Chris Taylor

Reputation: 53699

The grouped results are returned as an IEnumerable<IGrouping<TKey, T>>, which in your case will be IEnumerable<IGrouping<string, AllQuestionsPresented>>.

The code below shows how you can access the data in the grouping.

  var groupedByTopic = from question in forumData 
                       group question by question.Topic;

  foreach (var group in groupedByTopic)
  {
    Console.WriteLine(group.Key);
    foreach (var question in group)
    {
      Console.WriteLine("\t" + question.Name);
    }
  }

To create a dictionary from the above you can do the following

var groupingDictionary = groupedByTopic.ToDictionary(q=>q.Key, q=>q.ToList());

Which will give you a Dictionary<string, List<AllQuestionsPresented>>

If you went the LookUp route, which is nicely demonstrated by @wsanville , then you can get the dictionary the same way

  var lookup = forumData.ToLookup(q => q.Topic);
  var groupingDictionary = lookup.ToDictionary(q => q.Key, q => q.ToList());

Upvotes: 1

Nasir
Nasir

Reputation: 11401

var groupedByTopics = 
from n in forumData
group n by forumData.Topic into g
select new { Topic = forumData.Topic, Questions = g }

You may also want to keep this around for reference :-)

http://msdn.microsoft.com/en-us/vcsharp/aa336746

Upvotes: 1

wsanville
wsanville

Reputation: 37506

You can use ToLookup, which will give you a key/list of values collection. Your key will be the Topic, and you will get a list of AllQuestionsPresented for each key.

var lookup = forumData.ToLookup(f => f.Topic);

Reference on ToLookup

Upvotes: 4

harryovers
harryovers

Reputation: 3138

var groupedByPages =
    from n in forumData
    group n by forumData.Topic
    select n;

Upvotes: 0

Related Questions