SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    Need help with a Linq statement ASAP!

    Given the following class:

    Code:
        public class Question : Entity
        {
            public string Context { get; set; }
            public DateTime Version { get; set; }
            public string Content { get; set; }
            public string ChoiceA { get; set; }
            public string ChoiceB { get; set; }
            public string ChoiceC { get; set; }
            public string ChoiceD { get; set; }
            public string ChoiceE { get; set; }
            public char Correct { get; set; }
            public bool Changed { get; set; }
        }
    I need to select the most recent version of each Question.

    Code:
    IEnumerable<Question> mostRecent = allQuestions.What()????
    Field 'Context' will be the same among differing versions. I.E. Context might be 1234A for several previous versions, but each has a different time stamp.

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I completely misread. BRB.

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Question restated here a little better.
    http://forums.asp.net/p/1727143/4625...33469714572464

  4. #4
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hmm. Does this help? I'm a bit rusty so don't trust this.

    Code:
    static void Main(string[] args)
    {
    	Question[] allQuestions = {
    			new Question { Context = "1234a", Version = DateTime.Now.AddDays(-2)},
    			new  Question { Context = "1234a", Version = DateTime.Now.AddDays(-1)},
    			new  Question { Context = "1233a", Version = DateTime.Now.AddDays(-2)},
    			new  Question { Context = "1233a", Version = DateTime.Now.AddDays(-1)}
    								};
    
    
    	var mostRecent =
    		from q in allQuestions
    		orderby q.Version 
    		group q by q.Context into r
    		select new
    		{
    			Context = r.Key,
    			Version = r.Last().Version
    					
    		};
    
    	foreach (var i in mostRecent)
    		Console.WriteLine(i.Context + " * " + i.Version);
    }

  5. #5
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Sorry, not a bit. I don't understand it when written like that. I barely understand it at all. I need something more like the following, so that it return the right type.

    IEnumerable<Question> mostRecent = _context.Set<Question>().GroupBy(...).OrderBy(...)...blah blah

  6. #6
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do you mean you just need it written in lambda expression?
    Code:
    var mostRecent = allQuestions.OrderBy(q => q.Version).GroupBy(r => r.Context).Select(x => new { Context = x.Key, Version = x.Last().Version });
    Paging honeymonster. Paging honeymonster.

  7. #7
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Basically, but in this code "Select(x =>" x isn't an IEnumerable<Question>. Also, the returns aren't right. It only produces one row ever. I need the top row in each group. If table contains...

    1 - 4567 - 8/4/2011 6:23:28 PM
    2 - 1234 - 8/4/2011 6:23:28 PM
    3 - 4567 - 9/4/2011 6:23:28 PM
    4 - 1234 - 9/4/2011 6:23:28 PM
    5 - 4567 - 10/4/2011 6:23:28 PM
    6 - 1234 - 10/4/2011 6:23:28 PM

    I want to see (exactly)

    [Most Recent]
    6 - 1234 - 10/4/2011 6:23:28 PM
    5 - 4567 - 10/4/2011 6:23:28 PM

    [Previous Versions]
    4 - 1234 - 9/4/2011 6:23:28 PM
    2 - 1234 - 8/4/2011 6:23:28 PM
    3 - 4567 - 9/4/2011 6:23:28 PM
    1 - 4567 - 8/4/2011 6:23:28 PM

  8. #8
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Ok, I got what I wanted with the following horrible code, but I'm not satisfied. If somebody can translate this into a single line using the fluent api I'd be greatful.

    public static class Extensions
    {
    public static IEnumerable<Question> GetMostRecent(this IEnumerable<Question> questions)
    {
    var groups = questions.GroupBy(x => x.Context);
    foreach (var group in groups)
    {
    var sub = group.OrderByDescending(x => x.Version);
    yield return sub.First();
    }

    }
    public static IEnumerable<Question> GetPreviousVersions(this IEnumerable<Question> questions)
    {
    var groups = questions.GroupBy(x => x.Context);
    foreach (var group in groups)
    {
    var sub = group.OrderByDescending(x => x.Version);
    foreach (var item in sub.Skip(1))
    yield return item;
    }
    }
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •