奇怪的 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,因此 LINQ 语句本身不支持 Take 和 FirstOrDefault 之类的内容,我选择在迭代期间使用这些方法,因为它不会引发异常)
问题是,当所有 3 个字段(游戏、收件人和评论)都存在时。我收到异常“对象必须实现 IConvertible”。但如果我删除 3 个字段分配中的任何一个(无论是哪一个),它都可以正常工作。有人知道这是怎么回事吗?
提前致谢!
瑞安.
I am using a LINQ statement that selects from various tables information I need to fill some Post / Post Comment style records. I'm getting a funny exception saying that the object must implement IConvertible when I try to iterate the record set. The funny part about it is that it seems to only occur when the anonymous type I'm using to hold the data contains more than 2 generic collections.
//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 }
};
(Note: I am using MYSQL Connector/Net so things like Take and FirstOrDefault and things like that are not supported within the LINQ statements themselves, I have opted to use those methods during iteration as it does not raise an exception there)
The problem is, when all 3 fields (Game, Recipient, and Comments) are present. I get the exception "Object must implement IConvertible". BUT if I remove ANY one of the 3 field assignments (doesn't matter which one), it works just fine. Anybody know what's going on here?
Thanks in advance!
Ryan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个猜测,但我认为您的匿名类型可能会尝试duck-type当您有两个以上的查询时,将 转换为枚举。如果发生这种情况,那么您将收到有关未实现 IConvertible 的抱怨,因为枚举实现了 IConvertible,而您的匿名类型(现在从枚举派生)未实现该接口。
This is a guess, but I think your anonymous type might be attempting to duck-type to an enum when you've got more than two queries. If that's happening, then you'll get this complaint about not implementing IConvertible, as enum implements IConvertible, and your anonymous type (now derrived from enum) does not implement the interface.