Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 25th, 2011, 09:50 PM
Authorized User
 
Join Date: Dec 2011
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
Default Best way to concenate strings in LINQ to Entities?

I looked online and this was the best solution I could find but it's doesn't compile correctly.

Code:
     var myFight = from r in myEntity.FIGHTs
                    let WvsL = string.Format("{0} vs {1}", r.Winner, r.Loser)  
                    select new {
                    Match = WvsL,
                    r.Result,
                    r.RoundEnd,
                    r.TimeEnd,
                    r.FOTN,
                    r.KOTN,
                    r.SOTN,
                    r.TitleFight
                    };
I want to concatenate the 2 fighters names into one column called Match. My compiler error is as follows:

Code:
LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression.

What would be the best to go about this?


And I really appreciate the quick resonses so far. I have spent all of my schooling doing mostly C++ programming and this web stuff has so much syntax to learn it makes my head explode. lol
 
Old December 26th, 2011, 05:46 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,


For questions not directly related to my book - such as this one - please post in a more general ASP.NET / .NET category that you can find here: http://p2p.wrox.com/asp-net-4-539/ / http://p2p.wrox.com/

That said, LINQ to EF has trouble translating the String.Format into a SQL expression. What you can do is execute a EF query first (which results in the generated SQL) and then use LINQ to Objects to concatenate the values. Using the PlanetWrox database, something like this would work:

Code:
 
using (PlanetWroxEntities context = new PlanetWroxEntities())
{
  var reviews = (from r in context.Reviews
                where r.Authorized == true
                select new { r.Id, r.Title }).ToList();
 
  var concatenatedRviews = from r in reviews
                           select new
                           { 
                             r.Id,
                             Joined = string.Format("{0} - {1}", r.Id, r.Title)
                           };
  GridView1.DataSource = concatenatedRviews;
  GridView1.DataBind();
}
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
LINQ to EF and LINQ to SQL sophia BOOK: Beginning ASP.NET 4 : in C# and VB 1 October 19th, 2011 12:16 PM
resolving entities stolte XSLT 5 February 24th, 2011 01:31 PM
Linq Question about related entities gspro BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 March 23rd, 2010 09:29 AM
Comparing Performance Linq To entities and outofBand SQL updates urpcor BOOK: Professional ADO.NET 3.5 with LINQ and the Entity Framework ISBN: 978-0-470-22988-0 4 September 28th, 2009 06:22 AM
Entities safin XSLT 1 November 7th, 2005 04:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.