Linq to sql 在linq to sql中处理属性
这是我的第一个LINQtoSQL项目,所以我的问题肯定很幼稚。Linq to sql 在linq to sql中处理属性,linq-to-sql,Linq To Sql,这是我的第一个LINQtoSQL项目,所以我的问题肯定很幼稚。 到目前为止,我一直在每个DateTime属性旁边的业务对象中创建新属性,这是因为我需要对DateTime属性进行一些处理,并以特殊的字符串格式显示它,以便绑定到UI控件。例如: 私有日期时间_insertDate; /// ///我的数据库表中有“InertDate”字段 /// 公共日期时间插入日期 { 获取{return\u insertDate;} 设置{u insertDate=value;} } //因为我需要做一些
到目前为止,我一直在每个DateTime属性旁边的业务对象中创建新属性,这是因为我需要对DateTime属性进行一些处理,并以特殊的字符串格式显示它,以便绑定到UI控件。例如:
私有日期时间_insertDate;
///
///我的数据库表中有“InertDate”字段
///
公共日期时间插入日期
{
获取{return\u insertDate;}
设置{u insertDate=value;}
}
//因为我需要做一些处理,所以我创建了一个只读字符串属性,它将InsertDate传递给实用工具方法并返回特殊的字符串日期
公共字符串PInsertDate
{
获取{return Utility.ToSpecialDate(_insertDate);}
}
我的问题是我不知道如何在林克做到这一点。我确实喜欢跟踪,但我得到了运行时错误。“方法'System.String ToSpecialDate(System.Object)'不支持转换为SQL”
ToosDataContext db=新的ToosDataContext();
var newslist=来自p,单位为db.News
选择new{p.NewsId,p.Title,tarikh=MD.Utility.ToSpecialDate(p.ReleaseDate)}
GridView1.DataSource=新闻列表;
GridView1.DataBind();
您需要将在数据库中执行的查询与要在过程中执行的处理分开。试试这个:
var newslist = db.News
// First do a select in SQL
.Select(p => new {p.NewsId, p.Title, p.ReleaseDate})
.AsEnumerable() // Do the rest in process
.Select(p => new {p.NewsId, p.Title,
tarikh = MD.Utility.ToSpecialDate(p.ReleaseDate) });
你犯了什么错误?当你收到错误时,无论何时提出问题,请张贴错误是什么。张贴你收到的错误将非常有用 ToosDataContext db = new ToosDataContext(); var newslist = from p in db.News select new {p.NewsId,p.Title,tarikh =MD.Utility.ToSpecialDate( p.ReleaseDate)}; GridView1.DataSource = newslist; GridView1.DataBind();
var newslist = db.News
// First do a select in SQL
.Select(p => new {p.NewsId, p.Title, p.ReleaseDate})
.AsEnumerable() // Do the rest in process
.Select(p => new {p.NewsId, p.Title,
tarikh = MD.Utility.ToSpecialDate(p.ReleaseDate) });