Date post: | 02-Apr-2018 |
Category: |
Documents |
Upload: | divyasindhuri |
View: | 214 times |
Download: | 0 times |
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 1/23
EmployeeDesigner.cs
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>// This code was generated by a tool.
// Runtime Version:4.0.30319.225
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </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
{
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 2/23
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.EmployeeConnectionString, mappingSource)
{
OnCreated();
}
public EmployeeDataContext( string connection) :
base (connection, mappingSource)
{
OnCreated();
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 3/23
}
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 >();
}
}
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 4/23
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;
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 5/23
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
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 6/23
{
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
{
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 7/23
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();
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 8/23
}
}
}
[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_ProductAvailability" , Storage= "_ProductAvailability" , ThisKey= "ProductId" , OtherKey= "ProductId" ,IsUnique= true , IsForeignKey= false )]
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 9/23
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" );
}
}
}
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 10/23
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);
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 11/23
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
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 12/23
{
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" );
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 13/23
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" )]
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 14/23
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();
}
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 15/23
[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
{
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 16/23
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_ProductAvailability" , Storage= "_Product" , ThisKey= "ProductId" , OtherKey= "ProductId" , IsForeignKey= true )]
public Product Product
{
get
{
return this ._Product.Entity;
}
set
{
Product previousValue = this ._Product.Entity;
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 17/23
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;
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 18/23
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;
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 19/23
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 andmust implement IEnumerable
where word.Length <= 3
select word; //Every LINQ statement must mandatorily have from andselect. where is optional
//LINQ Statement to extract words with less than or equal to 3 chars. Both this andthe 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;
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 20/23
//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 tonumbers after the LINQ query.
// this is because the Linq query is not executed int the place where it is placed, itis only compiled.
// It will get executed only at foreach. A LINQ query will always be executed as lateas possible.
// If you want to execute it then and there then we can convert it into somecollection. 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 };
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 21/23
XDocument doc = XDocument .Load( "NumberList.xml" ); // in the propertie of xmlfile set "copy to output directories" as "Copy always"
var shortWords = from word in doc.Descendants( "word" ) // this statement meansthat 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 Studio2010\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;InitialCatalog=Employee;User ID=sa;Password=sa" );
var regions = from r in db.GetTable< Region >()
select r;
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 22/23
foreach (var item in regions)
Console .WriteLine(item.Description);
//using Linq to Sql class for this we must use usingLanguageEnhancements.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. andadd 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.
7/27/2019 Linq.rtf
http://slidepdf.com/reader/full/linqrtf 23/23
{
[Column (IsPrimaryKey= true )]
public int RegionId { get ; set ; } //property and column name are same so dont haveto specify column name
[Column (Name= "RegionDescription" )] // property is different from column name sowe specify the column name
public string Description { get ; set ; }
}
}