Two ways for Eager Loading in Linq to SQL
The first option is the DataLoadOptions, and the second is
to utilize a ViewModel class.
DataLoadOptions
The first one is easy and simple to code, but not very good if you want to write back data to the database.
Web.WebDB db = new Web.WebDB();
IEnumerable<Web.Model.WebsiteImage> websiteImages = db.WebsiteImages;
ViewModel Class
The second option is a little bit more work, but you can easily persist data to the database on post backs. This option requires you to create a custom class to hold the reference data.
[HttpGet]
public ActionResult WebsiteImages()
{
Web.WebDB db = new Web.WebDB();
WebViewModel webVM = new WebViewModel();
webVM.WebsiteImagesLinking = (from wi in db.WebsiteImages
select new WebsiteImagesVM
{
ID = wi.ID,
WebsiteImage = wi.WebsiteImage1,
Website = wi.Website,
WebsiteID = wi.WebsiteID,
WebsiteLanguage = wi.WebsiteLanguage,
Image = wi.Image,
ImageID = wi.ImageID,
MinWidth = wi.MinWidth,
MinHeight = wi.MinHeight,
MaxWidth = wi.MaxWidth,
MaxHeight = wi.MaxHeight,
Notes = wi.Notes
}).ToList();
return View(webVM);
}
public List<WebsiteImagesVM> WebsiteImagesLinking { get; set; }
public class WebsiteImagesVM
{
public int ID { get; set; }
public string WebsiteImage { get; set; }
public int WebsiteID { get; set; }
public Web.Model.Website Website { get; set; }
public Web.Model.WebsiteLanguage WebsiteLanguage { get; set; }
public int? ImageID { get; set; }
public Web.Model.Image Image { get; set; }
public int? MinWidth { get; set; }
public int? MinHeight { get; set; }
public int? MaxWidth { get; set; }
public int? MaxHeight { get; set; }
public string Notes { get; set; }
public bool Mapped { get; set; }
}