0

It throws this error

The entity or complex type 'OnStageTonight_MVC.Models2.GigViewModel' cannot be constructed in a LINQ to Entities query

when using this viewmodel

public class GigViewModel
    {
        [Key]
        public int gvmid { get; set; }
        public string bandname { get; set; }
        public string venuename { get; set; }
        public string address1 { get; set; }
        public string city { get; set; }
        public DateTime whatdate { get; set; }
        public TimeSpan starttime { get; set; }        
    }

And with this controller method

string user = User.Identity.GetUserId();
            var yourgigs = (from g in dbg.gigs
                            from v in dbg.Venues
                            from b in dbg.Bands
                            from ga in g.gigsaccasses
                            where (ga.Id == user &&
                            v.venueid == g.venueid &&
                            b.bandid == g.bandid)

                            select new GigViewModel
                            {
                                bandname = b.bandname,
                                venuename = v.venuename,
                                address1 = v.address1,
                                city = v.city,
                                whatdate = g.whatdate,
                                starttime = g.starttime
                            });

Apparently, it's something to do with not being able to project onto a mapped entity. So I tried projecting on to an anonymous type,

string user = User.Identity.GetUserId();
            var yourgigs = (from g in dbg.gigs
                            from v in dbg.Venues
                            from b in dbg.Bands
                            from ga in g.gigsaccasses
                            where (ga.Id == user &&
                            v.venueid == g.venueid &&
                            b.bandid == g.bandid)

                            select new
                            {
                                bandname = b.bandname,
                                venuename = v.venuename,
                                address1 = v.address1,
                                city = v.city,
                                whatdate = g.whatdate,
                                starttime = g.starttime
                            });

However, this threw the error about passing an anonymous type to a view that needs this specific model type

Where do I go from here?

DLeh
  • 21,934
  • 14
  • 72
  • 111
  • One thing is not clear for me : why Do you have a class named "ViewModel" which is mapped to an entity ? ViewModels has usually nothing to do with entities... – Raphaël Althaus May 27 '15 at 15:04

1 Answers1

0

The first one would produce an IQueryable<GigViewModel>, is that what your view is expecting?

The second one won't work because you can't pass anonymous types to views (as the error message would indicate).

That said, I would make sure you have navigation properties set on your entity model correctly, then just pass a List<gig> or IQueryable<gig>.

var result=dbg.gigs
  .Include(g=>g.Venues)
  .Include(g=>g.Bands)
  .Where(g=>g.gigsaccasses.Any(ga=>ga==user))
  .ToList();  /* Optional if your DbContext is defined controller wide */

Then modify the view to display data from your gigs object.

If that is too difficult (which you will probably save yourself a lot of headaches later if you do), then do project to a List of anonymous objects, then since it is now LINQ over objects, project again to your final class although I'm not sure why it didn't work in the first place.

string user = User.Identity.GetUserId();
            var yourgigs = (from g in dbg.gigs
                            from v in dbg.Venues
                            from b in dbg.Bands
                            from ga in g.gigsaccasses
                            where (ga.Id == user &&
                            v.venueid == g.venueid &&
                            b.bandid == g.bandid)

                            select new
                            {
                                bandname = b.bandname,
                                venuename = v.venuename,
                                address1 = v.address1,
                                city = v.city,
                                whatdate = g.whatdate,
                                starttime = g.starttime
                            })
                       .ToList()
                       .Select(g=>new GigViewModel
                            {
                                bandname=g.bandname,
                                venuename=g.venuename,
                                address1=g.address1,
                                city=g.city,
                                whatdate=g.whatdate,
                                starttime=g.starttime
                            });
Robert McKee
  • 20,124
  • 1
  • 35
  • 53
  • This latter option throws the error Cannot initialize type 'OnStageTonight_MVC.Models2.GigViewModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable' at the line .Select(g=>new GigViewModel { –  May 27 '15 at 21:50
  • Sorry, I can't explain that one. It's not a collection initializer, it's an object initializer. Do you have more than one GigViewModel in your project perhaps in a separate file? I've updated the answer to explicitly name the destination properties, but you shouldn't have to. Try removing the [Key] attribute and gvmid property as well from GigViewModel. – Robert McKee May 27 '15 at 23:19