Linq Query Return Error
33 观看
2回复
171 作者的声誉
I cannot figure out why my return from a linq query always is not correct.
[HttpGet]
public List<Programs> GetPrograms()
{
using (var context = new ProgramsDataContext1())
{
var qry = (from a in context.Programs
join b in context.courselist on a.Prog_id equals(b.prog_id)
where a.Prog_id ==b.prog_id
orderby a.Programname
select new
{ Prog_id =a.Prog_id,Programname=a.Programname, programlevel=a.programlevel, planname=a.planname, catalogNo=b.catalogNO, coursetitle=b.coursetitle, subject=b.subject
}
).ToList();
return qry ;
}
}
It always has a red underline on the qry. What is the problem here? Thanks.
作者: user788448 的来源 发布者: 2017 年 12 月 27 日回应 2
1像
12596 作者的声誉
Since you are doing the join already, you do not need the where
clause. Also your return type is not the same, that's the error you are getting. You are right now returning a list of anonymous
objects, not Programs
0像
188870 作者的声誉
Just define a class that lists just the properties you need from the two classes and instead of building an anonymous object create instances of this class.
public class ProgramsCourseDto
{
public int Prog_id {get;set;}
public string Programname {get;set;}
public int programlevel {get;set;}
public string planname {get;set;}
public int catalogNo {get;set;}
public string coursetitle {get;set;}
public string subject {get;set;}
}
[HttpGet]
public List<ProgramsCourseDto> GetProgramsCourses()
{
using (var context = new ProgramsDataContext1())
{
var qry = (from a in context.Programs
join b in context.courselist on a.Prog_id equals(b.prog_id)
orderby a.Programname
select new ProgramsCourseDto
{
Prog_id =a.Prog_id,
Programname=a.Programname,
programlevel=a.programlevel,
planname=a.planname,
catalogNo=b.catalogNO,
coursetitle=b.coursetitle,
subject=b.subject
}).ToList();
return qry ;
}
}
作者: Steve
发布者: 2017 年 12 月 27 日
来自类别的问题 :
- linq 关于DataTable的LINQ查询
- linq LINQ中的数据冲突
- linq 调试LINQ to SQL SubmitChanges()
- linq LINQ插入后我可以返回'id'字段吗?
- linq 如何在LINQ查询中使用GROUP BY获取MAX行?
- linq 是否可以使用LINQ透视数据?
- linq LINQ相当于foren for IEnumerable <T>
- linq 流利和查询表达 - 一个优于其他人的利益吗?
- linq 使用LINQ连接字符串
- linq IQueryable <T>和IEnumerable <T>有什么区别?
- linq 使用LINQ获得序列的奇/偶部分
- linq LINQ中的多个“ order by”
- linq 哪种方法表现更好:.Any()vs .Count()> 0?
- linq 如果不支持Contains,如何在LINQ to Entities(实体框架)中执行SQL样式的“IN”语句?
- linq 检查数组是否是另一个数组的子集
- linq LINQ for Java工具
- linq 在List <int>中查找最常出现的数字
- linq 使用LINQ更新集合中的所有对象
- linq 如何将新记录添加到IQueryable变量?
- linq 无法获取可更新的视图在Linq和Sql 2005中工作。