Saturday, April 3, 2010

Asp.Net repository class to be used with linq

using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;

///
/// Repository Main File for all Classes of Database
///

///


public class DonorRep
{
    private DataClassesDataContext db = new DataClassesDataContext();

    public IQueryable FindAllDonors()
    {
        return db.Donors;
    }

    public IEnumerable FindDistictCities()
    {
        var s = db.Donors.Select(p => p.City).Distinct();
        return s;
    }

    public Donor GetDonor(int id)
    {
        return db.Donors.SingleOrDefault(i => i.DonorId == id);
    }

    public Donor GetDonorByUserName(string UserName)
    {
        return db.Donors.SingleOrDefault(i => i.UserName == UserName);
    }

    public void Add(Donor donor)
    {
        db.Donors.InsertOnSubmit(donor);
    }

   //persistence
    public void Save()
    {
        db.SubmitChanges();
    }
}

No comments:

Post a Comment