Joe
Joe

Reputation: 4183

Linq query to store one to many collection in List object

I have two tables (Application and Hazard) with a one to many relationship.

I want to store the list of Hazards to a List object in my ViewModel.

applicationVm (greatly simplified to just show the collection):

 ...
 public List<Hazard> Hazards { get; set; }

LINQ query (greatly simplified to just show the collection):

  IQueryable<ApplicationVm> applVms;
  applVms = from app in _db.Applications
    ...
    join hz in _db.Hazards on app.Id equals hz.ApplicationId into hzr
      from hzrd in hzr.DefaultIfEmpty()
    select new ApplicationVm { ..., Hazards = hzrd };

Intellisense shows the error 'cannot covert source type Hazard to target type List<Hazard>' on the Hazards = hzrd in the select.

How do I write this LINQ query.

PS I do not want to return applVms as a list since I'm lazy loading.

Upvotes: 1

Views: 336

Answers (3)

Ivan Stoev
Ivan Stoev

Reputation: 205859

Here

from hzrd in hzr.DefaultIfEmpty()
select new ApplicationVm { ..., Hazards = hzrd };

The first line basically flattens the result of the group join, thus hzrd is of type Hazard.

To get the desired result, remove the first line and change the second like this

select new ApplicationVm { ..., Hazards = hzr.ToLIst() };

Upvotes: 1

tede24
tede24

Reputation: 2354

Why dont you use navigation property app.Hazards?

Anyway, this should work:

_db.Applications.GroupJoin(_db.Hazards, a=>a.Id, h=>h.ApplicationId,(a,hzds)=> new ApplicationVm {...,Hazards = hzds,...})

Also maybe your List should be IEnumerable (sorry I'm out of the computer so I couldn't test it)

Upvotes: 0

Dr. Stitch
Dr. Stitch

Reputation: 918

I tried it and it works.

class Program
    {
        static void Main(string[] args)
        {
            List<Application> application = new List<Application>();
            List<Hazard> hazard = new List<Hazard>();
            int appID = 1;
            int hazID = 1;
            for (int i = 0; i < 10; i++)
            {
                application.Add(new Application() { AppID = appID, AppName = string.Format("AppName{0}", i + 1) });
                hazard.Add(new Hazard() { HazID = hazID, AppID = appID, HazName = string.Format("HazName{0}", hazID) });
                hazID++;
                hazard.Add(new Hazard() { HazID = hazID, AppID = appID, HazName = string.Format("HazName{0}", hazID) });
                hazID++;
                appID++;
            }

            IEnumerable<AppHaz> appHaz = from app in application
                                   select new AppHaz { AppID = app.AppID, Hazards = (from haz in hazard where haz.AppID == app.AppID select haz).ToList() };

        }
    }

    class Application
    {
        public int AppID { get; set; }
        public string AppName { get; set; }
    }

    class Hazard
    {
        public int HazID { get; set; }
        public int AppID { get; set; }
        public string HazName { get; set; }
    }

    class AppHaz
    {
        public int AppID { get; set; }
        public List<Hazard> Hazards { get; set; }
    }

Upvotes: 2

Related Questions