连续两次被相同问题困扰。
一、举例
var query=db.StudentScore.Where(r=> r.SubjectId==subjectId).Select(g=>
new
{SubjectId=SubjectId,Score=g.Score}).ToList();
var query=db.StudentScore.Where(r=> r.SubjectId==subjectId).Select(g=>
new StudentScore
{SubjectId=SubjectId,Score=g.Score}).ToList();
var query=db.StudentScore.Where(r=> r.SubjectId==subjectId).Select(g=>
new StudentScoreTemp
{SubjectId=SubjectId,Score=g.Score}).ToList();
var query=db.StudentScore.Where(r=> r.SubjectId==subjectId).Select(g=>g
).ToList();
6 Select的时候,时间无法转换成 年月日 YYMMMdd
报错:LINQ to Entities 不识别方法“System.String ToString(System.String)”,因此该方法无法转换为存储表达式。
var dataTemp = T_User.Skip((p.page - 1) * p.rows).Take(p.rows).ToList();
var data = dataTemp.Select(d => new
{
d.UserID,
CreateTime= d.CreateTime.ToString("yyyy-MM-dd")
//CreateTime= d.InvestigationTime.Value.ToString("yyyy-MM-dd")
// 如果CreateTime 是可空值类型DateTime?或Nullable<System.DateTime> ,需要加Value。
});
public class IViewPageModel
{
public int page { get; set; }
public int rows { get; set; }
public string sort { get; set; }
public string order { get; set; }
public Filter[] filters { get; set; }
public string search { get; set; }
}
//1)查询SqlQuery
result.slList = db.Database.SqlQuery<DataTemp>("select Precision,Year,LandCover, sum(Area) as Area from [EcoTypes] where LandCover=@type group by Precision, Year, LandCover", new SqlParameter("@type", "森林")).Select(d=> d.Area).ToList();
//2)增删改:ExecuteSqlCommand
db.Database.ExecuteSqlCommand("").ToString();