1

I have the following table:

enter image description here

And the following data:

enter image description here

How can i filter the result, so that i only get the latest row from each omraade_id (sorted descending by timestamp)?

Which in this case would be the rows with id: 1010 and 1005

--

From @lazyberezovsky's answer, i have created the following expression:

dbConnection = new ElecEntities();

            var query = from data in dbConnection.Valgdata
            orderby data.timestamp descending
            group data by data.omraade_id into g
            select g.FirstOrDefault();

            return query.ToList();

It returns two rows with the ID 3 and 4, which are the first two rows in the database, and also the ones with the lowest timestamp. Any idea why?

5 Answers 5

2
var query = dbConnection.Valgdata
                        .GroupBy(x => x.omraade_id)
                        .Select(g => g
                             .OrderByDescending(x => x.timestamp)
                             .FirstOrDefault());
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for trying. This returns all 11 rows (returns row=3 9 times and row=4 2 times) ?
Thanks again. I appreciate the effort. This returns a Grouping object with two keys. The first key contains all the rows from the first omraade_id, and the second key contains all the rows from the second omraade_id (not sorted).
@Kenci I just tried and it worked fine for me. Are you sure you have the all parenthesis in the right place?
I have copy-pasted your answer. I have uploaded an image of what i get here: img202.imageshack.us/img202/6407/dbgroupquery.png
@Kenci you're right, I'm sorry. Apparently, I made a mistake while trying to convert what I have to the Linq syntax. See my update.
|
1

I have no experience with EF, so I'm unsure if only SQL-esque linq works here. A plain C#-ish:

var query = dbConnection.Valgdata.GroupBy(u => u.omraade_id)
      .Select(x => x.FirstOrDefault(y => x.Max(p => p.timestamp) == y.timestamp));

2 Comments

@Kenci why is it FirstOrDefault? First works in all cases! Because you will always have a maximum timestamp no matter what.
The timestamp column is nullable, and it throws an error when i execute the query with First() - but i should probably change this :)
0

You have put filter on every item. It should be applied on complete query result, not on every item.

Following is updated query.

 var query = (from data in dbConnection.Valgdata
        orderby data.timestamp descending
        group data by data.omraade_id into g
        select g).FirstOrDefault();

1 Comment

Thanks for the answer. I have tried your suggestion, it returns 9 results (all the rows with id = 6)
0
var query = from v in dbConnection.Valgdata
            orderby v.timestamp descending
            group v by v.omraade_id into g
            select g.First();

This will return only record with max timestamp for each omraade_id.

UPDATE query above works fine to me (at least for MS SQL Linq provider). Also you don't need to do FirstOrDefault - if omraade_id is grouped, then it definitely has at least one row.

var query = from v in dbConnection.Valgdata
            group v by v.omraade_id into g
            select g.OrderByDesc(x => x.timestamp).First();

2 Comments

It returns the same if i write descending or ascending
@Kenci see updated answer. Maybe its linq provider specific issue. Try to order records after grouping. That definitely should work.
0

This is my solution so far:

var data = dbConnection.Valgdata.Where(x => x.godkendt == false).ToList();
var dataGrouped = data.GroupBy(x => x.omraade_id).ToList();

List<Valgdata> list = new List<Valgdata>();

 foreach (var grpdata in dataGrouped)
            {
                var dataGroup = grpdata.OrderByDescending(x => x.timestamp).ToList();
                list.Add(dataGroup.FirstOrDefault());
            }
return list;

I dont know if it is the most effective, but it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.