1

My Job table has a one to many relationship with my JobTag table.

the data for a particular Job has many JobTag records.

However this query is not populating the JobTags.

    var query = from j in context.job.AsNoTracking().Include(j => j.JobTag)
                            join i in context.MYEXTItem on j.ItemID equals i.ItemID
                            where j.HeadID == orderId && i.TemplateID == 50 
                            select new { Job = j, Item = i };
                var jobitems = query.ToArray();
                var job1 = jobitems[0];
                var num = job1.Job.JobTag.Count;  // is Zero why?

num returns zero even when JobTags are present for the Job.

What am I doing wrong?

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • 1
    you are losing the child because you are selecting anonymous object, quick solution, add property JobTag in the select and you can make the count on job tag or first to get the job and so on – Monah Jul 16 '15 at 06:19
  • https://social.msdn.microsoft.com/Forums/en-US/3f39e481-3c72-4abe-80b7-c92427747011/include-does-not-work-with-join?forum=adodotnetentityframework seems to have some information about this – Kirsten Jul 16 '15 at 06:21
  • @HadiHassan, how to I do that? there are many JobTags for a Job. – Kirsten Jul 16 '15 at 06:22
  • yes, if you want only their counts , in your select put the count, if you need to access them put the object in the select, to get only job later, you can make the query.Select(t=>Job).Distinct(); and to browse the tags you can in 2 foreach loop, if you want i can show my idea as answer. – Monah Jul 16 '15 at 06:26
  • @HadiHassan. Yes please post your solution as the answer. – Kirsten Jul 16 '15 at 17:42

2 Answers2

1

you can do something like this

var details = from j in db.Jobs
                          join t in db.JobTags on j.Id equals t.JobId
                          select new
                          {
                              Job = j,
                              Tag = t,
                          };
            // if you want to get the list of jobs only
            var jobs = (from j in details
                        select j).Distinct();
            // if you want to get the counts
            var count = from d in details
                        group d by d.Job into g
                        select new
                        {
                            Job = g.Key,
                            TagCounts = g.Count()

                        };

hope that this will help you

Monah
  • 6,714
  • 6
  • 22
  • 52
1

See this.

You're both changing the shape of the query after the Include and you've got a projection. Not that this makes it more obvious. I think that this "lost Include problem" is very counter-intuitive.

Anyway, we have to live with it. In your case you could do something like...

select new { Job = j, Tags = j.JobTag, Item = i }

...or if you're only interested in the counts...

select new { Job = j, TagsCount = j.JobTag.Count(), Item = i }

...and remove the Include.

Community
  • 1
  • 1
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291