奇怪的LINQ到实体异常

时间:2011-05-05 20:10:33

标签: c# .net mysql linq linq-to-entities

我正在使用LINQ语句,从各种表中选择我需要填写一些帖子/帖子后评论样式记录的信息。我得到一个有趣的例外,当我尝试迭代记录集时,该对象必须实现IConvertible。关于它的有趣部分是它似乎只发生在我用来保存数据的匿名类型包含2个以上的泛型集合时。

//select friend timeline posts posts
var pquery = from friend in fquery
    join post in db.game_timeline on friend.id equals post.user_id
    //join user in db.users on post.friend_id equals user.id into userGroup
    //join game in db.games on post.game_id equals game.game_id into gameGroup
    select new
    {
        Friend = friend,
        Post = post,

        Game = from game in db.games
          where game.game_id == post.game_id
          select game,

        Recipient = from user in db.users
          where user.id == post.user_id
          select user,

        Comments = from comment in db.timeline_comments
          where comment.post_id == post.id
          join users in db.users on comment.user_id equals users.id
          select new { User = users, Comment = comment }
    };

(注意:我正在使用MYSQL Connector / Net,所以像Take和FirstOrDefault这样的东西在LINQ语句本身中不受支持,我选择在迭代期间使用这些方法,因为它不会引发异常)

问题是,当所有3个字段(游戏,收件人和评论)都存在时。我得到了异常“对象必须实现IConvertible”。但是,如果我删除3个字段分配中的任何一个(无关紧要),它就可以正常工作。谁知道这里发生了什么?

提前致谢!

瑞恩。

1 个答案:

答案 0 :(得分:1)

这是猜测,但我认为当您有两个以上的查询时,您的匿名类型可能会尝试duck-type到枚举。如果发生了这种情况,那么你会得到关于不实现IConvertible的抱怨,因为枚举实现了IConvertible,你的匿名类型(现在来自enum)没有实现接口。

相关问题