使用linq时获得异常

时间:2013-09-27 16:00:18

标签: c# mysql sql asp.net-mvc linq

这个问题(How to do multiple joins when using groupby in linq)不重复,情况已经发生变化。

我的数据库中有一张图片评分表。对于每个新评级,将创建新的图片评级表行。我按照上周和pictureid对这些评级进行分组,这意味着上周一张照片的所有评级都会累积并成为一个评级。然后我就可以按照他们的数量对它们进行排序。然后我加入了另外两张表,显示了用户和图片相关的细节。

这是我的代码:

public IQueryable<SortedDto> GetOne()
{
        var pastDate = DateTime.Now.Date.AddDays(-1);
        var combo = from l in db.picturerating
                    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  into first
                    join u in db.users on pgroup.FirstOrDefault().iduser equals u.iduser into second    
                    select new SortedDto
                    {
                        IdPictures = pgroup.FirstOrDefault().idpictures,
                        IdUser = pgroup.FirstOrDefault().iduser,
                        IdDatetime = first.FirstOrDefault().pictime,
                        totalrating = pgroup.Average(l => (float?)l.rating) ?? 0, sex =second.FirstOrDefault().sex,
                        username =second.FirstOrDefault().username,
                        dob = second.FirstOrDefault().dob

                    };

    return combo;
}

运行此代码时,我得到如下异常:

  

InnerException:{
  消息:“发生错误。”,
  ExceptionMessage:“'where子句'中的未知列'Apply1.idpictures'”,
  ExceptionType:“MySql.Data.MySqlClient.MySqlException”

     

堆栈跟踪:

     

at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
         at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow,Int64&amp; insertedId)          at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32&amp; affectedRows,Int64&amp; 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.ExecuteDbDataReader(CommandBehavior behavior)          在System.Data.Common.DbCommand.ExecuteReader(CommandBehavior行为)          在System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)

那我为什么得到这个例外?我的linq查询有什么问题吗?

0 个答案:

没有答案