I am a linq to sql fan. Decided to make a new app with linq to entities 4.1
All i want to do is return a date like so
var
who =
from
p in
db.viewLogs
where
p.lastAccessed >= today
select
new
{
name = p.name,
date = p.lastAccessed.Value.Hour,
pageName = p.pageName
};
what i really want is to return the date as a DATE not in seconds since time began.
Convert.Todatetime(p.lastaccessed) or anything like it fails. .TolongDateTime() fails... everything fails. All i can get is seconds since whatever.
Very annoying. Making me want to toss entity framework in the trash. Why is it Linq to Sql seems better, but linq to EF is what Microsoft is pushing when it seems it sucks bad. searches have come up with nothing, I have found other people that are frustrated
with linq to ef and some that have not found solutions.
No. That doesnt work. Thats my point with Linq to sql. It DOES work in linq to sql, but not in linq to entities.
base {System.SystemException} = {"LINQ to Entities does not recognize the method 'System.String ToLongDateString()' method, and this method cannot be translated into a store expression."}
The first tutorial series is for Web Forms and EF Database First and includes a date example in the second tutorial in the series.
The second tutorial series is for MVC and Code First and includes a date example in the second tutorial, and shows how to format it in the fourth tutorial.
leia
Member
96 Points
87 Posts
Linq to Entity Framework
Nov 08, 2011 03:28 PM|LINK
UGH.
I am a linq to sql fan. Decided to make a new app with linq to entities 4.1
All i want to do is return a date like so
var
who = from p in
db.viewLogs
where
p.lastAccessed >= today
select
new
{
name = p.name,
date = p.lastAccessed.Value.Hour,
pageName = p.pageName
};
what i really want is to return the date as a DATE not in seconds since time began.
Convert.Todatetime(p.lastaccessed) or anything like it fails. .TolongDateTime() fails... everything fails. All i can get is seconds since whatever.
Very annoying. Making me want to toss entity framework in the trash. Why is it Linq to Sql seems better, but linq to EF is what Microsoft is pushing when it seems it sucks bad. searches have come up with nothing, I have found other people that are frustrated with linq to ef and some that have not found solutions.
Bikram Saluj...
Participant
1582 Points
347 Posts
Re: Linq to Entity Framework
Nov 08, 2011 03:35 PM|LINK
try this to just get date part. hope this helps.
Convert.ToDateTime(p.lastAccessed).ToShortDateString()
-------------
Please remember to Mark As Answer if this post answered your question!
leia
Member
96 Points
87 Posts
Re: Linq to Entity Framework
Nov 08, 2011 04:02 PM|LINK
No. That doesnt work. Thats my point with Linq to sql. It DOES work in linq to sql, but not in linq to entities.
base {System.SystemException} = {"LINQ to Entities does not recognize the method 'System.String ToLongDateString()' method, and this method cannot be translated into a store expression."}
Bikram Saluj...
Participant
1582 Points
347 Posts
Re: Linq to Entity Framework
Nov 08, 2011 04:27 PM|LINK
ok. I see. Try this. It's a two step process though.
var
who = from p in
db.viewLogs
where
p.lastAccessed >= today
select
new
{
name = p.name,
date = p.lastAccessed,
pageName = p.pageName
};
var r = from c in who.AsEnumerable()
select
new
{
c.name,
ShortDate = c.date.ToShortDateString(),
c.pageName
};
hope this helps.
-------------
Please remember to Mark As Answer if this post answered your question!
Bikram Saluj...
Participant
1582 Points
347 Posts
Re: Linq to Entity Framework
Nov 08, 2011 04:38 PM|LINK
try this. this should work for you. it's a 2 step process to get what u want.
var
who = from p in
db.viewLogs
where
p.lastAccessed >= today
select
new
{
name = p.name,
date = p.lastAccessed,
pageName = p.pageName
};
var r = from c in who.AsEnumerable()
select
new
{
c.name,
ShortDate = c.date.ToShortDateString(),
c.pageName
};
-------------
Please remember to Mark As Answer if this post answered your question!
tdykstra
Contributor
4446 Points
622 Posts
Microsoft
Moderator
Re: Linq to Entity Framework
Nov 08, 2011 05:26 PM|LINK
Have you looked at the Entity Framework tutorials at
http://asp.net/entity-framework/tutorials
The first tutorial series is for Web Forms and EF Database First and includes a date example in the second tutorial in the series.
The second tutorial series is for MVC and Code First and includes a date example in the second tutorial, and shows how to format it in the fourth tutorial.
leia
Member
96 Points
87 Posts
Re: Linq to Entity Framework
Nov 08, 2011 05:38 PM|LINK
No, i have not gone through the tutorials. Numerous searches have come up with the solution of doing two seperate queries to "fix" this problem.
First - query the DB with linq to EF and pull the date.
Second - run another query over the results of the first and change the date.
This is.... crap? It works but its crap.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Linq to Entity Framework
Nov 10, 2011 01:46 AM|LINK
Hello leia:)
Because linq-to-entity will convert the C# or Visual Basic statements into standard sql statements and do execution. So you cannot do that directly.
You can try to use AsEnumerable() to convert from linq-to-sql to linq-to-object and have a try:
var who = from p in db.viewLogs.AsEnumerable()
where p.lastAccessed >= DateTime.Now
select new
{
name = p.name,
date = p.lastAccessed.Value.Hour,
pageName = p.pageName
};
Pagial
Member
4 Points
5 Posts
Re: Linq to Entity Framework
Apr 17, 2012 09:40 AM|LINK
Thank you so much decker dong... that worked perfectly
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Linq to Entity Framework
Apr 18, 2012 01:09 AM|LINK
Never mind!Welcome to our forum again to chat with us about the technoloy of ASP.NET……