load parent object to child wiht linq

I have 2 objects offer and campaign

Im trying to load the offer object into the campaign object when it loads, the problem is this

1) if i load it as IEnumrable it doesnt comply wiht the object type Offer
2) if i do IEnumerable<Offer>.ToSingle(), then i would expect it to hit the database at that point, which may not be optimal

any ideas on how this can work?
public class Campaign
    {
        public long ID { get; set; }
        public Offer Offer { get; set; }
    }
 
    public class Offer
    {
        public long ID { get; set; }
        public IList<Campaign> Campaigns { get; set; }
    }
 
        public IEnumerable<Campaign> GetCampaigns()
        {
            var campaigns = from c in _db.DbCampaignSet
                            let offer = GetOffers().Where(o => o.ID == c.slp_Offer.OfferID)
                    select new Campaign
                    {
                        ID = c.CampaignID,
                        Offer = offer <-- Problem here
                    };
            return campaigns;
        }
 
        public IEnumerable<Offer> GetOffers()
        {
            var offers = from o in _db.OfferSet
                         select new Offer
                         {
                             ID = o.OfferID
                         };
            return offers;
        }

Open in new window

LVL 8
rodmjayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi rodmjay;

I think this will do what you need, see code snippet. Then you can remove the other query in GetOffers().

Fernando
public IEnumerable<Campaign> GetCampaigns()
{
    var campaigns = from c in _db.DbCampaignSet
                    join o in _db.OfferSet on c.slp_Offer.OfferID equals o.OfferID
                    select new Campaign
                    {
                        ID = c.CampaignID,
                        Offer = o
            };
    return campaigns;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.