积极答复者
求 ef 语句

问题
-
框架:asp.net MVC3,使用EF处理数据,现在我有两个数据表,关系如下:
已知infoes(资讯)的一条记录 和 数据上下文对象,想通过一个方法求得与之相关的其他资讯(相关资讯使用与之关联的tag来做判断)这个时候的ef语句怎么写?
说明:数据上下文对象为 db
我现在的方法是这样的(这是DBSet Infoes 类下的一个方法):
/// <summary> /// 获取相关文章列表(相关性有资讯标签决定) /// </summary> /// <param name="db"></param> /// <returns></returns> /// <remarks></remarks> public List<Info> GetInfoesWithTags(ConnectionEntity db, short count = 10) { ArrayList tagsArray = new ArrayList(); List<Info> infoes = new List<Info>(); this.Tags.ForEach(a => tagsArray.Add(a.TagName)); infoes = db.InfoesTags.Where(a => tagsArray.Contains(a.TagName) & a.InfoID != this.InfoID).GroupBy(a => a.InfoID).Select(a => a.Select(b => b.Info)).Take(count); return infoes; }
结果出现如下错误:
“/”应用程序中的服务器错误。
无法将类型为“System.Data.Entity.Infrastructure.DbQuery`1[System.Collections.Generic.IEnumerable`1[ConnectionEntity.Info]]”的对象强制转换为类型“System.Collections.Generic.List`1[ConnectionEntity.Info]”。
人要诚实!还要踏实! 我先为人人,人人再为我! 我的Spaces: http://mydodu.spaces.live.com/
- 已编辑 Dodu.NET 2012年7月31日 14:29
- 已移动 Sheng Jiang 蒋晟Moderator 2012年7月31日 15:00 System.Data (发件人:ASP.NET 与 AJAX)
答案
-
Hi Dodu.NET,
看看这样是否可行:
infoes = db.InfoesTags.Where(a => tagsArray.Contains(a.TagName) & a.InfoID != this.InfoID).GroupBy(a => a.InfoID).Select(a => a.Info).Take(count).ToList();
Have a nice day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Allen_MSDNModerator 2012年8月10日 8:29
全部回复
-
这是设计的行为
如果你要执行DbQuery并将返回的结果填充一个List,用DbQuery的ToList方法。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
你好,先谢谢你的解答,但问题的所在不是tolist,而是我这个语句写的不对,但不知道怎么写,所以……
人要诚实!还要踏实! 我先为人人,人人再为我! 我的Spaces: http://mydodu.spaces.live.com/
-
Hi Dodu.NET,
看看这样是否可行:
infoes = db.InfoesTags.Where(a => tagsArray.Contains(a.TagName) & a.InfoID != this.InfoID).GroupBy(a => a.InfoID).Select(a => a.Info).Take(count).ToList();
Have a nice day.
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Allen_MSDNModerator 2012年8月10日 8:29