Need help with a Linq statement ASAP!

Given the following class:

    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.

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.

I completely misread. BRB. :stuck_out_tongue:

Question restated here a little better.
http://forums.asp.net/p/1727143/4625836.aspx/1?p=True&t=634533469714572464

Hmm. Does this help? I’m a bit rusty so don’t trust this.

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);
}

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

Do you mean you just need it written in lambda expression?

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. :slight_smile:

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

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&lt;Question&gt; GetMostRecent(this IEnumerable&lt;Question&gt; questions)
    {
        var groups = questions.GroupBy(x =&gt; x.Context);

foreach (var group in groups)
{
var sub = group.OrderByDescending(x => x.Version);
yield return sub.First();
}

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