Showing posts with label LinqToSql. Show all posts
Showing posts with label LinqToSql. Show all posts

Monday, July 29, 2013

Eager Loading in Linq To Sql : Two ways

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.

[HttpGet]
public ActionResult WebsiteImages()
{
    Web.WebDB db = new Web.WebDB();
    DataLoadOptions options = new DataLoadOptions();
    options.LoadWith<Web.Model.WebsiteImage>(x => x.Website);
    options.LoadWith<Web.Model.WebsiteImage>(c => c.WebsiteLanguage);
    db.LoadOptions = options;
  
    IEnumerable<Web.Model.WebsiteImage> websiteImages = db.WebsiteImages;
    return View(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 { getset; }


public class WebsiteImagesVM
    {
        public int ID { getset; }
        public string WebsiteImage { getset; }
        public int WebsiteID { getset; }
        public Web.Model.Website Website { getset; }
        public Web.Model.WebsiteLanguage WebsiteLanguage { getset; }
        public int? ImageID { getset; }
        public Web.Model.Image Image { getset; }
        public int? MinWidth { getset; }
        public int? MinHeight { getset; }
        public int? MaxWidth { getset; }
        public int? MaxHeight { getset; }
        public string Notes { getset; }
        public bool Mapped { getset; }
    }








Thursday, May 30, 2013

Getting column value - Bad vs. Good Linq to Sql

Bad
int mainDepID = db.Departments.SingleOrDefault(y => y.ID == depID).MainDepartmentID;

Good
int mainDepID = 0;

Web.Model.Department currentDepartment = db.Departments.SingleOrDefault(y => y.ID == depID);

if (currentDepartment != null)
{
   mainDepID = currentDepartment.MainDepartmentID;
}

Friday, May 24, 2013

Cool Stuff: Use array to query values using Linq to Sql

You can use an array to query for records using linq to sql...

 int[] myList = new int[] { 2625, 2666 };
 
 var myResult = from st in db.SomeTable          
               where myList.Contains(st.ID)      
               select st;
 
 
Might be useful sometimes?

Wednesday, March 27, 2013

Validating Linq to Sql Model

If you have created a Linq to Sql class as your model and need to add validation to it, simply create an interface and add the columns needed to validate.

ILog interface class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
 
namespace Test1.Models
{
    interface ILog
    {
        [Required]
        [RegularExpression(@"(\s|.){1,50}$", ErrorMessage = "Field cannot contain more than 50 characters")]
        string Name { getset; }
 
        [Required]
        [RegularExpression(@"[0-9]*", ErrorMessage = "Field must be an integer")]
        int TableKey { getset; }
 
        [Required]
        [RegularExpression(@"(\s|.){1,50}$", ErrorMessage = "Field cannot contain more than 500 characters")]
        string Value { getset; }
    }
}



Log LinqToSql class:

using Test1.Models;
using System.ComponentModel.DataAnnotations;
 
namespace Log.Model
{
    [MetadataType(typeof(ILog))]
    partial class Log : ILog
    {
    }
}

Simple enough?

For larger models, I suggest creating a ViewModel and handle everything in there...