Processing a property in linq to sql
It's my first LINQ TO SQL Project , So definitely my question could be naive .
Till now I used to create new property in Business Object beside of every DateTime Property , That's because i need to do some processing in my DateTime property and show it in special string format for binding to UI Controls .Like :
private DateTime _insertDate;
///
/// I have "InertDate" field in my Table on Database
///
public DateTime InsertDate
{
get { return _insertDate; }
set { _insertDate = value; }
}
// Because i need to do some processing I create a readonly string property that pass InsertDate to Utility method and return special string Date public string PInsertDate { get { return Utility.ToSpecialDate(_insertDate); } }
My question is I don't know how to do it开发者_运维技巧 in LINQ . I did like follow but i get run time error. "Method 'System.String ToSpecialDate(System.Object)' has no supported translation to SQL"
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();
You need to separate out the query performed in the database from the processing to be done in process. Try this:
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) });
精彩评论