You are on page 1of 23

EmployeeDesigner.

cs
#pragma warning disable 1591 //-----------------------------------------------------------------------------// <auto-generated> // // // // // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. This code was generated by a tool. Runtime Version:4.0.30319.225

// </auto-generated> //------------------------------------------------------------------------------

namespace LanguageEnhancements.DataAccess { using System.Data.Linq; using System.Data.Linq.Mapping; using System.Data; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; using System.ComponentModel; using System;

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="Employee")] public partial class EmployeeDataContext : System.Data.Linq.DataContext {

private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

#region Extensibility Method Definitions partial void OnCreated(); partial void InsertProduct(Product instance); partial void UpdateProduct(Product instance); partial void DeleteProduct(Product instance); partial void InsertRegion(Region instance); partial void UpdateRegion(Region instance); partial void DeleteRegion(Region instance); partial void InsertProductAvailability(ProductAvailability instance); partial void UpdateProductAvailability(ProductAvailability instance); partial void DeleteProductAvailability(ProductAvailability instance); #endregion

public EmployeeDataContext() :

base(global::LanguageEnhancements.Properties.Settings.Default.EmployeeConnectionStri ng, mappingSource) { OnCreated(); }

public EmployeeDataContext(string connection) : base(connection, mappingSource) { OnCreated();

public EmployeeDataContext(System.Data.IDbConnection connection) : base(connection, mappingSource) { OnCreated(); }

public EmployeeDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); }

public EmployeeDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); }

public System.Data.Linq.Table<Product> Products { get { return this.GetTable<Product>(); } }

public System.Data.Linq.Table<Region> Regions { get { return this.GetTable<Region>(); } }

public System.Data.Linq.Table<ProductAvailability> ProductAvailabilities { get { return this.GetTable<ProductAvailability>(); } } }

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Products")] public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged {

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

private int _ProductId;

private string _ProductName;

private string _ProductDescription;

private int _ProductCount;

private EntityRef<ProductAvailability> _ProductAvailability;

#region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnProductIdChanging(int value); partial void OnProductIdChanged(); partial void OnProductNameChanging(string value); partial void OnProductNameChanged(); partial void OnProductDescriptionChanging(string value); partial void OnProductDescriptionChanged(); partial void OnProductCountChanging(int value); partial void OnProductCountChanged(); #endregion

public Product() { this._ProductAvailability = default(EntityRef<ProductAvailability>); OnCreated(); }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductId", DbType="Int NOT NULL", IsPrimaryKey=true)] public int ProductId

{ get { return this._ProductId; } set { if ((this._ProductId != value)) { this.OnProductIdChanging(value); this.SendPropertyChanging(); this._ProductId = value; this.SendPropertyChanged("ProductId"); this.OnProductIdChanged(); } } }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductName", DbType="NVarChar(50) NOT NULL", CanBeNull=false)] public string ProductName { get { return this._ProductName; } set {

if ((this._ProductName != value)) { this.OnProductNameChanging(value); this.SendPropertyChanging(); this._ProductName = value; this.SendPropertyChanged("ProductName"); this.OnProductNameChanged(); } } }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductDescription", DbType="NVarChar(50) NOT NULL", CanBeNull=false)] public string ProductDescription { get { return this._ProductDescription; } set { if ((this._ProductDescription != value)) { this.OnProductDescriptionChanging(value); this.SendPropertyChanging(); this._ProductDescription = value; this.SendPropertyChanged("ProductDescription"); this.OnProductDescriptionChanged();

} } }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductCount", DbType="Int NOT NULL")] public int ProductCount { get { return this._ProductCount; } set { if ((this._ProductCount != value)) { this.OnProductCountChanging(value); this.SendPropertyChanging(); this._ProductCount = value; this.SendPropertyChanged("ProductCount"); this.OnProductCountChanged(); } } }

[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Product_ProductAvailabili ty", Storage="_ProductAvailability", ThisKey="ProductId", OtherKey="ProductId", IsUnique=true, IsForeignKey=false)]

public ProductAvailability ProductAvailability { get { return this._ProductAvailability.Entity; } set { ProductAvailability previousValue = this._ProductAvailability.Entity; if (((previousValue != value) || (this._ProductAvailability.HasLoadedOrAssignedValue == false))) { this.SendPropertyChanging(); if ((previousValue != null)) { this._ProductAvailability.Entity = null; previousValue.Product = null; } this._ProductAvailability.Entity = value; if ((value != null)) { value.Product = this; } this.SendPropertyChanged("ProductAvailability"); } } }

public event PropertyChangingEventHandler PropertyChanging;

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void SendPropertyChanging() { if ((this.PropertyChanging != null)) { this.PropertyChanging(this, emptyChangingEventArgs); } }

protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Region")] public partial class Region : INotifyPropertyChanging, INotifyPropertyChanged {

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

private int _RegionId;

private string _RegionDescription;

#region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnRegionIdChanging(int value); partial void OnRegionIdChanged(); partial void OnRegionDescriptionChanging(string value); partial void OnRegionDescriptionChanged(); #endregion

public Region() { OnCreated(); }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RegionId", DbType="Int NOT NULL", IsPrimaryKey=true)] public int RegionId { get { return this._RegionId; } set

{ if ((this._RegionId != value)) { this.OnRegionIdChanging(value); this.SendPropertyChanging(); this._RegionId = value; this.SendPropertyChanged("RegionId"); this.OnRegionIdChanged(); } } }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RegionDescription", DbType="NVarChar(50)")] public string RegionDescription { get { return this._RegionDescription; } set { if ((this._RegionDescription != value)) { this.OnRegionDescriptionChanging(value); this.SendPropertyChanging(); this._RegionDescription = value; this.SendPropertyChanged("RegionDescription");

this.OnRegionDescriptionChanged(); } } }

public event PropertyChangingEventHandler PropertyChanging;

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void SendPropertyChanging() { if ((this.PropertyChanging != null)) { this.PropertyChanging(this, emptyChangingEventArgs); } }

protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.ProductAvailability")]

public partial class ProductAvailability : INotifyPropertyChanging, INotifyPropertyChanged {

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

private int _ProductId;

private int _Status;

private EntityRef<Product> _Product;

#region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnProductIdChanging(int value); partial void OnProductIdChanged(); partial void OnStatusChanging(int value); partial void OnStatusChanged(); #endregion

public ProductAvailability() { this._Product = default(EntityRef<Product>); OnCreated(); }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductId", DbType="Int NOT NULL", IsPrimaryKey=true)] public int ProductId { get { return this._ProductId; } set { if ((this._ProductId != value)) { if (this._Product.HasLoadedOrAssignedValue) { throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); } this.OnProductIdChanging(value); this.SendPropertyChanging(); this._ProductId = value; this.SendPropertyChanged("ProductId"); this.OnProductIdChanged(); } } }

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Status", DbType="Int NOT NULL")] public int Status {

get { return this._Status; } set { if ((this._Status != value)) { this.OnStatusChanging(value); this.SendPropertyChanging(); this._Status = value; this.SendPropertyChanged("Status"); this.OnStatusChanged(); } } }

[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Product_ProductAvailabili ty", Storage="_Product", ThisKey="ProductId", OtherKey="ProductId", IsForeignKey=true)] public Product Product { get { return this._Product.Entity; } set { Product previousValue = this._Product.Entity;

if (((previousValue != value) || (this._Product.HasLoadedOrAssignedValue == false))) { this.SendPropertyChanging(); if ((previousValue != null)) { this._Product.Entity = null; previousValue.ProductAvailability = null; } this._Product.Entity = value; if ((value != null)) { value.ProductAvailability = this; this._ProductId = value.ProductId; } else { this._ProductId = default(int); } this.SendPropertyChanged("Product"); } } }

public event PropertyChangingEventHandler PropertyChanging;

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void SendPropertyChanging() { if ((this.PropertyChanging != null)) { this.PropertyChanging(this, emptyChangingEventArgs); } }

protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } #pragma warning restore 1591

Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using System.Data.Linq; using LanguageEnhancements.DataAccess;

namespace LanguageEnhancements { class Program { static void Main(string[] args) { // LinqToXml(); // LinqToObjects(); LinqToSql(); } public static void LinqToObjects() {//uses System.Core.dll which is by default in the references //Linq to Objects => datasource is in the form of objects. string[] words = { "one", "two", "three", "four", "five", "six" }; List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//LINQ Statement to extract words with less than or equal to 3 chars. var shortWords = from word in words// words should balways be a collection and must implement IEnumerable where word.Length <= 3 select word; //Every LINQ statement must mandatorily have from and select. where is optional //LINQ Statement to extract words with less than or equal to 3 chars. Both this and the above are same. var otherWay = words.Where(w => w.Length <= 3).Select(w => w);

var evens = from num in numbers where num % 2 == 0 orderby num descending select num;

//can also be written as follows var evens1 = evens.Where(e => e % 2 == 0).Select(e => e);

numbers.Add(10);// the o/p will contain 10 also even though 10 is added to numbers after the LINQ query. // this is because the Linq query is not executed int the place where it is placed, it is only compiled. // It will get executed only at foreach. A LINQ query will always be executed as late as possible. // If you want to execute it then and there then we can convert it into some collection. ex:var evens = (from num in numbers //where num % 2 == 0 //orderby num descending // select num).ToArray();

foreach (var e in evens) { Console.WriteLine(e); } string[] colors = { "green", "brown", "blue", "red" }; Console.WriteLine(colors.Max(c => c.Length));//o/p is 5 //Console.WriteLine(colors.OrderBy(c=>c.Length).Single()); throws an exception var query = colors.Where(c => c.Contains("e")); query = query.Where(c => c.Contains("n")); Console.WriteLine(query.Count());// o/p is 1 } public static void LinqToXml() { //we should include using System.Xml.Linq; List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

XDocument doc = XDocument.Load("NumberList.xml");// in the propertie of xml file set "copy to output directories" as "Copy always"

var shortWords = from word in doc.Descendants("word") // this statement means that i want to explore all the decendents of the <word> tag where word.Value.Length <= 3 select word.Value;

foreach (var e in shortWords) { Console.WriteLine(e); }

//method 2

XElement numDoc = new XElement("Numbers", from num in numbers select new XElement("Number", num)); numDoc.Save(@"C:\Users\SLK\Documents\Visual Studio 2010\Projects\LanguageEnhancements\LanguageEnhancements\bin\Debug\Numbers.xml" ); } public static void LinqToSql() {// must have "using System.Data.Linq;" to use DataCOntext. DataContext db = new DataContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=Employee;User ID=sa;Password=sa");

var regions = from r in db.GetTable<Region>() select r;

foreach (var item in regions) Console.WriteLine(item.Description);

//using Linq to Sql class for this we must use using LanguageEnhancements.DataAccess; EmployeeDataContext db1 = new EmployeeDataContext(); db1.Log = Console.Out;

var productList = from p in db1.Products select p; foreach (var p in productList) Console.WriteLine(p.ProductName); } } }

Region.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq.Mapping;

namespace LanguageEnhancements { //linking this class to sql for this we should add System.Data.Linq to the references. and add using System.Data.Linq.Mapping; [Table] //table and class names are same so no need to mention table name public class Region //this is the entity class for Region Table.

{ [Column(IsPrimaryKey=true)] public int RegionId { get; set; } //property and column name are same so dont have to specify column name [Column(Name="RegionDescription")] // property is different from column name so we specify the column name public string Description { get; set; }

} }