开发者

Apply substring on values in LINQ

I am binding the dropdown values using LINQ as below

var varResult =
        (from OppData in dtOpp.AsEnumerable()
          select new
          {
           TEXT = OppData.Field<object>(sColName), //column value
           VALUE = sColName // column Name
          开发者_开发问答 }
        ).Distinct();

Then the code is converted to datatable using LINQtoDataTable Function.

dtTemp = LINQToDataTable(varResult);

Then the dropdown is binded as seen below;

 ddlTemp.DataSource = dtTemp;
 ddlTemp.DataTextField = "TEXT";
 ddlTemp.DataValueField = "VALUE";
 ddlTemp.DataBind();

Now the value that dropdown binds for one of the column, (Employee Joining date) is in format of 08/09/2011~Y, because it is directly getting bind from database. I wish to apply substring on the date so that it is in format of 08/09/2011. How to apply substring on the LINQ queries?


Use your TEXT field in your anonymous type in LINQ query as string and format value during query:

var varResult =
    (from OppData in dtOpp.AsEnumerable()
      select new
      {
       TEXT = OppMilestonedate.Field<object>("EMP_JOIN_DATE") == null ? null : OppMilestonedate.Field<object>("EMP_JOIN_DATE").ToString().Substring(0, 10), 
       VALUE = sColName 
      } 
    ).Where(x => x.TEXT != null).Distinct(); 

Somethign like this. Also you can provide more meaningful format of date representation, ex: Aug 09, 2011, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜