tags:

views:

28

answers:

1

Say I have the following Linq 2 SQL command;

ItemsRepository.All().Where(r => r.type == "myType");

Let's say it returns an object collection of;

  • id
  • title
  • description
  • type
  • etc

Is there a way to return the entire object and trim the description to the 1st 200 chrs if there are more than 200 chrs in the description?

Or should i get the collection and then itterate through them?

thanks

+1  A: 

You could use Select to project that to a new collection. So you'd have -

var x = ItemsRepository.All().Where(r => r.type == "myType");

var y = x.Select(z => new { z.id, z.title, z.description.Substring(0,200), z.type });

If you don't want to project to an anonymous type, just specify the type you wish to project to -

var y = x.Select(z => new MyNewType { ID = z.id, Title = z.title, Description = z.description.Substring(0,200), z.type });
Frank Tzanabetis
+1 but i'd rather not do this as the object is quite large. but i'm realising that this may need to be the way even so.
griegs
Well when you enumerate over y and it generates the sql, linq-to-sql will generate SQL to retrieve only what you ask for. So it'll only retrieve the fields that you specify with the Select operator, and the Substring(0,200) will be translated to the equivalent SQL.
Frank Tzanabetis
Yeah I get all that I was just hoping that there was a way to get the data trimmed. I have opted for a partial view that has trimmedText as a property so i use that where required. thanks for you help
griegs