last day (14 days later) » 

2:14 PM
1
A: Group by complex database set

Dmitry PopovAssuming that valid must be a positive number: var participantsWithBestResults = db.Participantes.Where(x => x.Results.Any(y => y.valid > 0)) .Select(x => new { user = x, bestResult = x.Results.Where(y => y.valid > 0) .GroupBy(y => y.type) .Select(y => new { ...

 
Yes, it will be 1 for valid. How would i now order it for the type of the result ?
 
So you need results, and not users?
 
I need to show the user and count which was the type of question he answered most. Let's say he answered 3 valid questions of type A, 1 of type B and 4 of type C. Then i would need to show 4 and C
 
Well, you should have written your question that way.
 
Sorry, i thought it was kind of clear. Is it possible ?
And by the way it didn't work, i still have results that have valid as 0
 
2:14 PM
It filters participants that have Any valid result. Do you need only those participants that have only valid results?
 
Ohh, no. What i need is all the participants and their results but only the results that are in the rule i explained earlier.
Hey
 
I updated my answer, and our discussion is now a chat
 
great, i just saw that ! i'll test it
 
I've tested my linq query in linqpad, it works and returns an anonymous class with two properties, one being user, and the other the best result (another anonymous class)
 
It didn't work, when i debugged i got "The 'First' method can only be used as a final consult operation, instead use FirstOrDefault
 
2:21 PM
Yes, that's what I expected
 
and when i changed it to First i only got some participants
i don't get it :/
 
I get it :) Now, I'll think how to fix it
 
thanks very much buddy
 
That was obvious, really. You can use .Key of the group. I updated the answer.
 
Really ? Didn't know that :/
ehh, it worked by i still get only 7 participants
 
2:34 PM
Ok, should I explain it step by step?
 
ok
 
db.Participantes.Where(x => x.Results.Any(y => y.valid > 0)) // only participants with ANY valid results (if no valid results, participant is ignored)
.Select(x => new // create an instance of anonymous class (we're not going to use this class anywhere else, so there's no need to have a name for it
 
that's the thing, i was looking at it !
 
Ok, so should I stop here?
 
sorry for interrupt you buddy
i need all the participants but also need to show their results, if the guy doesn't have any i will show 0
 
2:38 PM
No, that's alright
 
man, this thing is too complicated :D
 
Yes, quite complicated ))
Not as easy as I thought at the first glance
 
me too, i'm quite new at this linq thing
if it was only sql no problem, but this :/
i removed the two where and now i have all the users
 
Second Where is needed. You only count the valid results, don't you?
 
yes, i only need to count the valid
but if he doesn't have any valid i need to show 0 as the count and empty or whatever on type
 
2:47 PM
        .DefaultIfEmpty()
        .Select(y => new
        {
            type = y != null ? y.Key : "(string for no type)",
            count = y != null ? y.Count() : 0
        })
 
where should i put that ? after the first where or replace it ?
 
Replace the last select with this
You see, if there's no valid results, Where(valid > 0) will return empty collection, there will be nothing to group, so DefaultIfEmpty is needed, it will return a null group
 
Its not possible to compare elements of type 'System.Linq.IGrouping`2'. Only primitive and entity types
i see
 
You've messed it up, I think it is an exception for OrderBy
 
var participantsWithBestResults = db.Participantes.Where(x => x.Resultados.Any(y => y.valida > 1))
.Select(x => new
{
user = x,
bestResult = x.Resultados.Where(y => y.valida > 1)
.GroupBy(y => y.tipo)
.OrderByDescending(y => y.Count())
.DefaultIfEmpty()
.Select(y => new
{
type = y != null ? y.Key : "NO TIPO",
count = y != null ? y.Count() : 0
})
.FirstOrDefault()
});
should i remove it ?
nope, didn't work
 
2:55 PM
No, no, I don't see what's wrong
var participantsWithBestResults = db.Participantes
.Select(x => new
{
user = x,
bestResult = x.Resultados.Where(y => y.valida > 1)
.GroupBy(y => y.tipo)
.OrderByDescending(y => y.Count())
.DefaultIfEmpty()
.Select(y => new
{
type = y != null ? y.Key : "NO TIPO",
count = y != null ? y.Count() : 0
})
.FirstOrDefault()
});
 
same thing
damn it :/
 
        .GroupBy(y => y.type)
        .DefaultIfEmpty()
        .OrderByDescending(y => y != null ? y.Count() : 0)
What if you change order like that?
Both examples work for me, but I'm using objects, not a database
 
Same
But mine also is a object
 
What is your db variable?
 
db.Participantes
db is my context variable and Participantes is my DbSet
the Participantes has a FK with Resultados, so when u populate the Participantes u also get all their results
 
3:08 PM
I'm no expert in Linq queries to database, what I mean is that I use a collection of users and results created by hand to test my query and it works. So the error you get might be related to the fact that you use a database.
Try debugging it step by step. You can download LINQPad from linqpad.net
And use it to test your queries
 
ohh, i get it
 
It is freemium (no autocomplete in free version)
It can connect to database
 
im gonna try that buddy
thanks anyway for the help and your time
 
And you can insert .Dump() in between linq methods to see the intermediate results
You're welcome
 
:D
thanks
 
3:12 PM
LINQPad is an awesome application, you have to try it. I've been using it daily since I've found it.
It's not only for LINQ but for everything. (e.g. today I used it to show maps in wpf from geojson I needed to modify)
Good luck with debugging, thanks for marking my answer
 
thanks for the tip buddy
i'm gonna take a look at it
no problem
you helper me a lot
;)
 

last day (14 days later) »