How to do inner join on three tables, when one of the tables is using group by. I am able to write this query but it returns error on run time. So how should I modify my query.
var pastDate = DateTime.Now.Date.AddDays(-1);
var query = from l in db.picturelikes
where l.iddatetime > pastDate
group l by l.idpictures into pgroup
let count = pgroup.Count()
orderby count descending
join p in db.picturedetails on pgroup.FirstOrDefault().idpictures equals p.idpictures
join u in db.users on pgroup.FirstOrDefault().iduser equals u.iduser
select new SortedDto
{
IdPictures = pgroup.FirstOrDefault().idpictures,
IdUser = pgroup.FirstOrDefault().iduser,
totalrating = pgroup.Average(l => (float?)l.likenumber) ?? 0, // pl.Where(a => a.likenumber!= null).Average(c => c.likenumber)
sex =u.sex,
username =u.username,
dob = u.dob
};
return query;
This is the exception i get with my present code,
InnerException: { Message: "An error has occurred.", ExceptionMessage: "Unknown column 'Project2.idpictures' in 'where clause'", ExceptionType: "MySql.Data.MySqlClient.MySqlException", StackTrace: " at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDatoiaReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)" }
Is my query legit? Is it the way to do three inner joins when using groupby.
pgroup.Key
instead ofpgroup.FirstOrDefault().idpictures
– Sergey Berezovskiyp in db.picturedetails
is never used. – Andrew Coonce