Mark
Mark

Reputation: 7818

Linq query working in LinqPad but not in VS

var rooms = tblRoom
        .Where(r => r.hotel_id==1)
        .GroupBy(p => p.tblType)
        .Select(g => new
            {
            TypeName = g.Key.type_name,
            TypeID = g.Key.type_id,
            TypeCount = g.Count()
            });

            rooms.Dump();

In LinqPad - this works fine, and returns: LinqPadScreenShot

However, when I transfer this to Visual Studio:

var rooms = dbr.Rooms
                .Where(r => r.hotel_id == 1)
                .GroupBy(p => p.type_id)
                .Select(g => new 
                {
                    TypeName = g.Key.type_name,
                    TypeID = g.Key.type_id,
                    TypeCount = g.Count()
                });

I receive the message on the g.Key.type_name section:

**'long' does not contain a definition for 'type_name' and no extension method 'type_name' accepting a first argument of type 'long' could be found (are you missing a using directive or an assembly reference?) **

I am trying to add the results to the following viewmodel:

    public class RatesViewModel
    {
        public string TypeName { get; set; }
        public long TypeID { get; set; }
        public int TypeCount { get; set; }
    }

I'm sure once again, it must be something simple I'm missing - so any help in getting the results into the view model, would be appreciated.

Thanks, Mark

Upvotes: 2

Views: 471

Answers (3)

Justin Niessner
Justin Niessner

Reputation: 245429

You have two issues. First, your LINQ Query is creating an Enumeration of Anonymous objects. You just need to modify your query to specify the type to solve that issue.

The next is that you're trying to assign the results of the query to a single object rather than an Enumeration of objects. To fix that issue, you just need to tack on a First() (or Single(), FirstOrDefault(), or SingleOrDefault() depending):

.Select(g => new RatesViewModel
{
    TypeName = g.Key.type_name,
    TypeID = g.Key.type_id,
    TypeCount = g.Count()
}.First()

Upvotes: 1

cjk
cjk

Reputation: 46425

Look at the GroupBy lines in Linqpad vs. Visual Studio. You are grouping by different things.

In the first, you are grouping by an object, but in the second you are grouping by a number. The Key is the object being grouped, so calling g.key.type_name doesn't work when you are grouping by a number.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038900

I am trying to add the results to the following viewmodel:

I don't see how you are relating to this view model in your code. You are simlpy querying your repository and returning an anonymous object.

If you want to return a view model, adapt your query to project to this view model:

var rooms = dbr
    .Rooms
    .Where(r => r.hotel_id == 1)
    .GroupBy(p => p.type_id)
    .Select(g => new RatesViewModel
    {
        TypeName = g.Key.type_name,
        TypeID = g.Key.type_id,
        TypeCount = g.Count()
    })
    .ToList();
return View(rooms);

and now your view will obviously be strongly typed to the view model:

@model IEnumerable<RatesViewModel>

Upvotes: 3

Related Questions