您的位置:首页 > 其它

Linq之select不能返回其类型的解决方案

2009-08-14 18:01 239 查看
public IEnumerable<ChildCate> GetChildCates()
{
var dc=new Models.DbContext();
var s = from t in dc.GetTable<ChildCate>()
where t.ParentID == ID
orderby t.ID descending
select new { t.Tag, t.Name, table = t };
return s;
}
上面会报错吧
解决方案新建一个实体类
public class Category
{
public int ID { get; set; }
public string Tag { get; set; }
public string Name { get; set; }
}
然后将第一段的代码改为:

public IEnumerable<Category> GetChildCates()
{
var dc=new Models.DbContext();
var s = from t in dc.GetTable<ChildCate>()
where t.ParentID == ID
orderby t.ID descending
select new Category{Tag=t.Tag,Name=t.Name,ID=t.ID};
return s;
}

大功告成:
支持小第网站:http://www.cnyolee.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐