简体   繁体   中英

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

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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM