Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a data table in db.

C#
ID || ProductName  || SoldDateTime
==================================
1  || Books         || 6543213571
2  || Pencils       || 1594563215
3  || Paint         || 5411865446
4  || Markers       || 2421375125
5  || Copies        || 5546423010
.  || .             || 7568714312


I added it in my project as ProdutEntityModel

Now i want to convert SoldDateTime into 01/24/2015 and the resultant i want List<product> how can i achieve this.

VB
ID || ProductName  || SoldDateTime ||      SDateTime
==========================================================
1  || Books         || 6543213571  || 01/01/2015  06:22:15
2  || Pencils       || 1594563215  || 01/08/2015  15:25:55
3  || Paint         || 5411865446  || 01/15/2015  23:45:32
4  || Markers       || 2421375125  || 01/22/2015  00:56:33
5  || Copies        || 5546423010  || 02/12/2015  18:04:50
.  || .             || .           || .


How can i do this in Linq to Entity.
Posted
Updated 11-Jan-15 0:15am
v2
Comments
Tomas Takac 11-Jan-15 6:18am    
What does the number in SoldDateTime mean? Is it ticks or what?
shaprpuff 11-Jan-15 6:21am    
this is unix Numbers in seconds i converted it like this way,

DateTime s_date;
s_date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
s_date = s_date.AddSeconds(Convert.ToDouble(item.SoldDateTime )).ToLocalTime();
Zoltán Zörgő 11-Jan-15 6:36am    
1) code first or database first?
2) you want to add this calculated field to the table or to the model?
shaprpuff 11-Jan-15 7:11am    
this is database first approach i want to add this new column into the existing list

List<Products> list=ctx.Products.ToList();

i want to add this new column list.
Zoltán Zörgő 11-Jan-15 8:36am    
Supposing you have downvoted my answer, may I ask you why?

 
Share this answer
 
Comments
Maciej Los 11-Jan-15 11:48am    
+5! Sounds reasonable ;)
BillWoodruff 11-Jan-15 12:21pm    
+5 That's a very good tutorial with excellent use of graphic design, and clear writing. Too bad the author uses bit-maps for all the code, and gives you no way to download it.
As per my understanding, you can achieve that using 2 ways:
1. add partial class
See solution 1 by Zoltan Zorgo[^]
or
2. add not mapped field to your model
C#
[NotMapped]
public DateTime SDateTime {get; set;}


But you tag your question: Linq, so it might means you want to create query based on your model...

C#
var qry = from emd in EntityModelData
        select new {
            //get collection of your fields
            ID = ID,
            ProductName = ProductName,
            SoldDateTime = SoldDateTime,
            SDateTime = YourExtensionMethodToConvert(SoldDateTime)
        };
 
Share this answer
 
v2
Step1: Select only the required data.

C#
 List<products> prodlist = ctx.Products
                          .Where(
                          x => SqlFunctions.DateAdd("s", x.SoldDateTime , "1970-01-01").Value.Year==DateTime.Now.Year &&
                           SqlFunctions.DateAdd("s", x.SoldDateTime , "1970-01-01").Value.Month==DateTime.Now.Month  ).ToList();
                                               
                                              
</products>


Step2: I added Partial Class and not Mapped Property

C#
namespace MyShop
{
  public partial class Products
  {
    private DateTime s_date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
    public DateTime SDateTime
    { 
      get 
      {
        return s_date.AddSeconds(Convert.ToDouble(this.SoldDateTime)).ToLocalTime();
      }
    } 
  }
}




I would like to thanks "Zoltan Zorgo" for this help.
 
Share this answer
 
v4
Comments
Zoltán Zörgő 12-Jan-15 5:47am    
You could have simply done this

namespace MyShop
{
public partial class Products
{
private DateTime s_date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public DateTime SDateTime
{
get
{
return s_date.AddSeconds(Convert.ToDouble(item.SoldDateTime )).ToLocalTime();
}
}
}
}

This way you keep deferred execution.
shaprpuff 12-Jan-15 6:03am    
Good it works Thanks.
Maciej Los 12-Jan-15 6:41am    
My virtual 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900