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
Solved

LINQ and ToString

Posted on 2014-03-02
18
289 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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 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 63

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 63

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 63

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 63

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 80

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 63

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
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899404
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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