views:

587

answers:

3

I have an object with two different integer properties in it, and I'm trying to get a a new object in Linq to Entities, combining two integer properties from the same object as concatenated strings, as follows

List<DateRange> collection = (from d in context.dates 
select new DateRange 
{ 
  DateString = from s in context.Seasons 
  where s.SeasonID = d.DateID 
  select string.Format("{0} - {1}", s.StartYear, s.EndYear) }

).ToList<DateRange>(); 

The string concatenation of the years will not compile.

+2  A: 

This will work in LINQ to Objects, provided that each object in objects is a class or struct containing "Number1" and "Number2" fields or properties:

var results = from o in objects
              select string.Format("{0} - {1}", o.Number1, o.Number2);

(However, your original should work, as well....)

Reed Copsey
These are great suggestions. I'm using Linq to Entities. I guess my situation is more complex than I outlined. Here's basically the setup:List<DateRange> collection = (from d in context.dates select new DateRange { DateRange = from s in context.Seasons where s.SeasonID = d.DateID select string.Format("{0} - {1}", s.StartYear, s.EndYear) }).ToList<DateRange>(); The string concatenation will not compile here.
Bob_Kruger
+1  A: 

Your original code works if you really want what you wrote. However, if your really want to get from

var objects = new MyObject[]{ 
    new MyObject {Int1 = 1, Int2 = 2},
    new MyObject {Int1 = 3, Int2 = 4}};

something like 1 - 2 - 3 - 4 you can write

var strings = objects.Select(o = > string.Format("{0} - {1}", o.Int1, o.Int2).ToArray();
var output = string.Join(" - ", strings);
+1  A: 

Assuming you are connecting to a database via LINQ to SQL/Entities, then the String.Format call will likely fail, as with those providers, the select clause is executed within the database. Not everything can be translated from C# into SQL.

To convert your database results into a string like you want to, the following should work:

var temp = (
  from d in context.dates 
  from s in context.Seasons 
  where s.SeasonID == d.DateID 
  select new { s.StartYear, s.EndYear }
).ToList(); // Execute query against database now, before converting date parts to a string

var temp2 = 
  from t in temp 
  select new DateRange 
  { 
    DateString = t.StartYear + " - " + t.EndYear 
  };

List<DateRange> collection = temp2.ToList();

EDIT: I had an additional thought. The String.Format call is most likely the problem. I am not sure if it would work or not, but what about a plain-jane concat:

List<DateRange> collection = 
(from d in context.dates 
 select new DateRange 
 { 
   DateString = from s in context.Seasons 
   where s.SeasonID = d.DateID 
   select s.StartYear + " - " + s.EndYear
 }
).ToList<DateRange>(); 
jrista
I will try this and report back.
Bob_Kruger
Yes, this works. The ToList is essential, as you surmised, to return the resultset first and then manipulate it outside the database with the second object. I wish my mind were as organized as yours!
Bob_Kruger
Ha! Well, I am not sure its so organized. ;P I just went through hell working with EF v1.0 in the past, and I encountered pretty much every problem it has. This was a fairly common one. If you have the option, I HIGHLY recommend moving to EV v4.0 (and .NET 4.0) as soon as possible, because it is a FAR more capable ORM than EF v1 or L2S. It also supports a lot more transformations as well as custom db function mappings, so things like String.Format (or a custom similar implementation) could actually work.
jrista
Cool, thanks. I will look into upgrading. Man it's hard to keep up!
Bob_Kruger