tags:

views:

30

answers:

1

In LinqPad I have the following query statements...

var genre = "Anime & Animation";
var minRating = 3.0;
var topNum = 30;

var query = 
    (from g in Genres
    from t in g.Titles 
    where g.Name==genre
    && t.Instant.Available==true
    && t.AverageRating >= minRating
    orderby t.AverageRating descending 
    select new {t.Name, t.Rating, t.AverageRating, t.ShortSynopsis}).Take(topNum);

query.Dump(string.Format("Top {0} {1} Instant Watch Movies with a {2:0.0} minimum average rating sorted by average rating in descending order.",topNum,genre,minRating));

I want to return the genre string variable in the result set.

Another way to ask, with TSQL I would do "SELECT field1, field2, 'Action & Adventure' as genre from MyTitles". So how do you do that in Linq.

Seth

+1  A: 

You can specify additional members of the anonymous type by adding PropertyName = <value>, so if you want to add a property Genre with some constant value, you can write:

new { t.Name, t.Rating, t.AverageRating, 
      t.ShortSynopsis, Genre = "Action & Adventure" }

The value of genre will be probably the same as the genre variable or the property g.Name in your query, so you could use this property (instead of string literal):

new { t.Name, t.Rating, t.AverageRating, 
      t.ShortSynopsis, Genre = g.Name } // Or 'Genre = genre'

It is worth noting that if you just use some property name in the construction of anonymous type (e.g. t.Name), it is just a shortcut for writing Name = t.Name (the compiler copies the property name automatically).

Tomas Petricek
Tomas, that's it. Thanks.
Seth Spearman