todd.pund
todd.pund

Reputation: 689

How to pull one column from second table in Linq query with join

I have the following linq query that works fine, but I am wanting to pull one column (CompanyId) from context.Emps into the results along with the results from context.BillingProfiles. How would I modify the select (select prof) below to include said column?

var query = (from prof in context.BillingProfiles
             join emp in context.Emps on prof.ID equals emp.ID
             join grp in context.BillingGroups on prof.GroupID equals grp.GroupID
             where (prof.EndDate == null) && (grp.System == "sysGrp") && (prof.ID == id)
             select prof).Distinct()
            .Select(x => new OpId()
            {
                id = x.ID,                                        
                GroupId = x.GroupID,
                OpId = x.OpID,
                StartDate = x.StartDate,
                EndDate = x.EndDate,
                AddedOn = x.AddedOn,
                AddedBy = x.AddedBy,
                RemovedOn = x.RemovedOn,
                RemovedBy = x.RemovedBy,
                Prodid = x.ProdID,
            });

Thanks

Upvotes: 0

Views: 2050

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

Project an anonymous object containing those too:

var query = from prof in context.BillingProfiles
            join emp in context.Emps on prof.ID equals emp.ID
            join grp in context.BillingGroups on prof.GroupID equals grp.GroupID
            where prof.EndDate == null && prof.ID == id && grp.System == "sysGrp"
            select new { prof, emp.CompanyId, grp };

Upvotes: 1

Related Questions