Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

LINQ and ToString

Posted on 2014-03-02
18
Medium Priority
?
296 Views
Last Modified: 2016-02-10
Hi Experts,

I have query which works fine but only when I don't use ToString()

Here it is and I need to make sure I can format Timestamp

myout.AddRange((from o in context.a
                                     join od in context.b on o.t_id equals od.id
                                     select new MyInfo
                                     {

                                         TId = od.id,
                                         //Timestamp = od.some_dte == null ? "" : od.some_dte.Value.ToString("yyyy/MM/dd HH:mm tt"),
                                        


                                     }).ToArray());

Open in new window



Thanks.
0
Comment
Question by:fpoyavo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +1
18 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39898940
What error do you receive?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39898943
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39898958
Since LINQ-to-SQL and Entity Framework ultimately translate your LINQ statements into actual SQL queries, you are bound by the confines of SQL. There is no SQL equivalent of ToString, so you'll need to execute that method outside of your EF query. Just follow up your query with another LINQ statement--one that executes only on your system and not on the database server.

e.g.

var query = (from o in context.a
             join od in context.b on o.t_id equals od.id
             select new MyInfo
             {
                 TId = od.id,
                 Timestamp = od.some_dte,
             }).ToArray()

var mapped = (from item in query
              select new
              {
                  TId = item.TId,
                  Timestamp = item.Timestamp == null ? "" : item.Timestamp.Value.ToString("yyyy/MM/dd HH:mm tt"),
              }).ToArray();

myout.AddRange(mapped);

Open in new window


The first ToArray call--the one that you originally had--will force the database query to execute. The second query therefore is working with data on your machine, and not on the database server.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39898998
Hi fpoyavo;

I am going to assume that you are using linq to SQL or Linq to EF and in that dialect of Linq ToString is not supported and therefore you must format the string on the local machine as shown in the modified query below.

myout.AddRange((from o in context.a
                join od in context.b on o.t_id equals od.id
                select new
                {
                    od.id,
                    od.some_dte
                }).ToList()
                  .Select( od => new MyInfo
                   {
                       Tid = od.id,
                       Timestamp = od.some_dte == null 
                                     ? "" 
                                     : od.some_dte.Value.ToString("yyyy/MM/dd HH:mm tt")
                   }).ToArray());
                

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39899001
Isn't that what I said?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39899006
@kaufmed, yes you did but I did not see your post until after I had posted mine.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899034
Ok. The problem is if I remove formating in first part it gives error. Timestamp in my class is string and column is datetime and that is why I used formatting in the first place :))

Please advise.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39899045
Did you try my solution, I think it should work seeming I did not use you custom class until the end where it is already a string.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899056
Why would I do conversion to string if it was datetime? Your code does not help.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39899063
To your question, "Why would I do conversion to string if it was datetime? Your code does not help.", because that is what you asked to do in the original question, has that changed?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899127
I recommend you to read my question again.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39899143
Ok, what is ment by this, "I have query which works fine but only when I don't use ToString()"' ?
0
 
LVL 83

Expert Comment

by:David Johnson, CD, MVP
ID: 39899173
The problem is if I remove formating in first part it gives error. Timestamp in my class is string and column is datetime and that is why I used formatting in the first place

why not using the date properties of the datetime ?  The database has it as a datetime object? But you manipulate it as a string?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899201
Ok so may you change my code to show how to make it work please? :)
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899368
Fernando yes I manipulate it as string that was clear from question I hope.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39899376
So if Timestamp in your class MyInfo is a string then what is my posted solution not doing for you? Is it giving an exception?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899392
Hi Fernando,

You code is almost ok except I am having problem with another thing but it will be another new question.

Thanks.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question