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
{
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
相关文章推荐
- IntelliSense: 调用约定后面不能是嵌套声明符, IntelliSense: 应输入类型说明符 concrt.h 解决方案
- 只能靠参数而不能靠返回值类型的不同来区分重载函数
- “SELECT TOP N *”语句中“N”不能为变量的解决方案
- ThinkPHP调用存储过程不能返回结果集的解决方案
- vc++ error C2533:“*”构造函数不能有返回类型
- EF:The provider 程序不能返回a ProviderManifest 实例 - (解决方案)
- Linq技巧2——限制返回数据中的继承类型
- LINQ查询返回DataTable类型
- 动态执行SQL语句,“SELECT TOP N *”语句中“N”不能为变量的解决方案
- jdbc 查询对象不能返回为空解决方案
- C#只能靠参数而不能靠返回值类型的不同来区分方法重载
- 关于Column '*' not found 解决方案 Hibernate使用SQL查询返回实体类型,即返回某个类,或实体类
- SWFUpload与CI不能正确上传识别文件MIME类型解决方案
- 谁说LINQ复杂查询不支持返回实名类型~复杂结果集中再使用复杂结果集
- “HtmlSelect”不能有类型为“LiteralControl”的子级
- C# 使用linq处理返回带datetime类型数据 json显示/date(xxxxx)/
- asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excel-xls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;
- 关于构造函数不能有返回类型的错误
- “HtmlSelect”不能有类型为“LiteralControl”的子级
- LINQToSQL中如何更好的手动设置导航字段,并返回实名类型而不是匿名类型