Friday, October 8, 2010

Jquery1



Friday, October 1, 2010

LINQ Coding

LINQ coding
-------------------------------
//from c in MurSapeliCovers
//join o in MurCompCustomers on new { cid = c.CustomerId, coid=c.CoverId} equals new
//{ cid = o.CustId, coid=o.CoverId} into g
//from o in g.DefaultIfEmpty()
//select new {Cover = c.Cover,
// compNumber = o == null ? 0 : o.CompNumber
// }


from p in MurCompApplications
join o in MurCompCustomers on p.CompNumber equals o.CompNumber
join c in MurSapeliCovers on new { cid = o.CustId, coid=o.CoverId} equals new
{ c.CustomerId = cid, CoverId=coid} into g
from c in g.DefaultIfEmpty()
select new {Cover = c.Cover,
compNumber = o == null ? 0 : o.CompNumber
}

from c in MurSapeliCovers
join o in ( from cu in MurCompCustomers join ca in MurCompApplications
on cu.CompNumber equals ca.CompNumber
select new { CustId = cu.CustId, CoverId = cu.CoverId, CompNumber=ca.CompNumber } )
on new { cid = c.CustomerId, coid=c.CoverId} equals new
{ cid = o.CustId, coid=o.CoverId} into g
from o in g.DefaultIfEmpty()
select new {Cover = c.Cover,
compNumber = o == null ? 0 : o.CompNumber
}


from p in MurCompApplications
join q in MurCompCustomers on p.CompNumber equals q.CompNumber
select new
{
coverid = q.CoverId, compNumber = p.CompNumber
}
///////////////////////////////////
from p in MurCompApplications
join q in MurCompCustomers on p.CompNumber equals q.CompNumber into g
from q in g.DefaultIfEmpty()
select new
{
coverid = q == null ? 0 : q.CoverId,
compNumber = p.CompNumber
}
///////////////////////////////////
from c in MurSapeliCovers
join o in MurCompCustomers on new { cid = c.CustomerId, coid=c.CoverId} equals new
{ cid = o.CustId, coid=o.CoverId} into g
from o in g.DefaultIfEmpty()
select new {Cover = c.Cover,
compNumber = o == null ? 0 : o.CompNumber
}
///////////////////////////////////

from c in MurSapeliCovers
join o in ( from cu in MurCompCustomers join ca in MurCompApplications
on cu.CompNumber equals ca.CompNumber
select new { CustId = cu.CustId, CoverId = cu.CoverId, CompNumber=ca.CompNumber } )
on new { cid = c.CustomerId, coid=c.CoverId} equals new
{ cid = o.CustId, coid=o.CoverId} into g
from o in g.DefaultIfEmpty()
select new {Cover = c.Cover,
compNumber = o == null ? 0 : o.CompNumber
}
from sapelicovers in TblCoverWithCustomerDetails
join compData in
(
from compCust in Comp_Customers
join compApp in Comp_Applications
on compCust.CompNumber equals compApp.Comp_CompNumber.CompNumber
where compApp.Active == true
&& compApp.Comp_TypeDetails.TypeId == 1
&& compApp.Comp_TypeDetails.Code == 1
select new
{
CompNumber = compCust.CompNumber,
CustId = compCust.CustId,
CoverId = compCust.CoverId,
IncidentDate = compApp.IncidentDate,
Active = compApp.Active,
CompTypeId = compApp.Comp_TypeDetails.TypeId,
CompTypeCode = compApp.Comp_TypeDetails.Code
}
)
on
new { coverno = sapelicovers.CoverNo , custid = sapelicovers.CustomerNo }
equals new { coverno = compData.CoverId , custid = compData.CustId }
into outer
from compData in outer.DefaultIfEmpty()
where sapelicovers.CustomerNo == 1
select new
{
Cover = sapelicovers.Cover,
InsuranceNumber = sapelicovers.InsuranceNo,
CompensationNumber = compData == null ? 0 : compData.CompNumber,
StatusCode = sapelicovers.CoverStatus,
EvaluationDate = compData.IncidentDate,
ExpiryReason = sapelicovers.ExpiryReason,
ValidFromDate = sapelicovers.CoverStartDate,
ValidToDate = sapelicovers.CoverEndDate,
SumInsured = sapelicovers.SumInsured,
Bonus = sapelicovers.BonusType,
Benificiary = sapelicovers.BeneficiaryCode
}

from sapelicover in TblCoverWithCustomerDetails
join o in
(
from p in Comp_Customers
join q in Comp_Applications
on p.CompNumber equals q.Comp_CompNumber.CompNumber
where q.Comp_TypeDetails.TypeId == 1
&& q.Comp_TypeDetails.Code == 1
&& p.CompNumber == 10005
&& q.Active == false
select new
{
coverid = p.CoverId,
CompNumber = p.CompNumber,
AddInfoReceivedDate = q.AddInfoReceivedDate,
AppReceivedDate = q.AppReceivedDate,
AddiInfo = q.Comments,
IncidentDate = q.IncidentDate,
CustId = p.CustId,
InsuranceNumber = p.InsuranceId
}
)
on new { coverid = sapelicover.CoverNo, custid = sapelicover.CustomerNo }
equals new { coverid = o.coverid, custid = o.CustId } into outer
from o in outer.DefaultIfEmpty()
where
sapelicover.Active == true
&& sapelicover.CustomerNo == 1
&& sapelicover.CoverNo == 1
select new
{
sapelicover, o
}














C# Expression
-------------
from p in Comp_Applications select p
from p in Comp_Applications select p.CompNumber
from p in Comp_Applications select new{p.RowId,p.CompNumber}
from p in Comp_Applications select new{Comp_Number=p.CompNumber}
from p in Comp_Applications select new{ rowid= p.RowId, comp_number= p.CompNumber}
from p in Comp_Applications where p.RowId > 10 select p
from p in Comp_Applications where p.CompNumber==10011 select p
from p in Comp_Applications where p.RowId > 10 && p.CompNumber==10011 select p
from p in Comp_Applications where p.RowId > 10 || p.CompNumber==10011 select p
from p in Comp_Applications orderby p.CreatedBy,p.CompNumber select p
from p in Comp_Applications group p by p.CreatedBy into g select new{ crea=g.Key}
from p in Comp_Applications group p by p.CreatedBy into g select new{ crea=g.Key, count=g.Count()}


var one = from p in Comp_Applications select p;
var two = from p in Comp_Applications select new{ rowid = p.RowId , compNumber = p.CompNumber };
one.Dump();
two.Dump();

from p in Comp_Applications
select new { p.CompNumber, CompeNumber = p.CompNumber > 10005 && p.CompNumber < 10008 ? 1 :
p.CompNumber > 10009 && p.CompNumber < 10020 ? 0:
p.CompNumber,
recDate = p.AppReceivedDate
}


outer join example:-
============================

create table MurCustomer
(
keyId int, customerName varchar(50)
)
create table MurOrder
(
keyId int, OrderNumber varchar(50)
)
insert into MurCustomer values(1,'xx');
insert into MurCustomer values(2,'yy');
insert into MurCustomer values(3,'zz');
insert into MurCustomer values(4,'aa');
insert into MurCustomer values(5,'bb');

insert into MurOrder values(1,'pppp');
insert into MurOrder values(1,'qqqq');
insert into MurOrder values(4,'rrrr');
insert into MurOrder values(5,'ssss');
insert into MurOrder values(5,'tttt');

select * from MurOrder;
select * from MurCustomer;


from c in MurCustomers
join o in MurOrders on c.KeyId equals o.KeyId into g
from o in g.DefaultIfEmpty()
select new {customerNameDisplay = c.CustomerName,
OrderNumber = o == null ? "(no orders)" : o.OrderNumber}


another example:-
===================
create table MurSapeliCover
(
CustomerId int, CoverId int, Cover varchar(50)
)
create table MurCompApplication
(
CompNumber int
)
create table MurCompCustomer
(
CompNumber int, CustId int, CoverId int
)

insert into MurSapeliCover values(1,1,'First cust 1st Cover');
insert into MurSapeliCover values(1,2,'First cust 2nd Cover');
insert into MurSapeliCover values(1,3,'First cust 3rd Cover');
insert into MurSapeliCover values(1,4,'First cust 4th Cover');

insert into MurSapeliCover values(2,1,'Second cust 1st Cover');
insert into MurSapeliCover values(2,5,'Second cust 2nd Cover');
insert into MurSapeliCover values(3,6,'Third cust 1st Cover');
insert into MurSapeliCover values(4,7,'Fourth cust 1st Cover');

insert into MurCompApplication values(1);
insert into MurCompApplication values(2);
insert into MurCompApplication values(3);

insert into MurCompCustomer values(1,1,1);
insert into MurCompCustomer values(2,1,2);
insert into MurCompCustomer values(3,2,1);


select * from MurSapeliCover;
select * from MurCompCustomer;
select * from MurCompApplication;

from c in MurSapeliCovers
join o in MurCompCustomers on new { cid = c.CustomerId, coid=c.CoverId} equals new
{ cid = o.CustId, coid=o.CoverId} into g
from o in g.DefaultIfEmpty()
select new {Cover = c.Cover,
compNumber = o == null ? 0 : o.CompNumber
}


from c in MurSapeliCovers
join o in ( from cu in MurCompCustomers join ca in MurCompApplications
on cu.CompNumber equals ca.CompNumber
select new { CustId = cu.CustId, CoverId = cu.CoverId, CompNumber=ca.CompNumber } )
on new { cid = c.CustomerId, coid=c.CoverId} equals new
{ cid = o.CustId, coid=o.CoverId} into g
from o in g.DefaultIfEmpty()
select new {Cover = c.Cover,
compNumber = o == null ? 0 : o.CompNumber
}

from p in Comp_Customers where p.CustId == 100
select new { p, p.CompNumber, p.CustId }


ClaimsCRUD.GetCustomerSapeliCovers(request);
*/

Sample LINQ Methods

public void SampleMethod()
{
DTOServices.Claims.Address FromAddress = new Sapeli.DTOServices.Claims.Address();
FromAddress.AddressKey = Guid.NewGuid();
FromAddress.City = "Bangalore";
FromAddress.Country = "India";
FromAddress.PAddress = "Mainroad, Marathahalli";
FromAddress.PostalCode = "560037";


DTOServices.Claims.Address ToAddress = new Sapeli.DTOServices.Claims.Address();

PropertyInfo[] propFrom = FromAddress.GetType().GetProperties();
PropertyInfo[] propTo = ToAddress.GetType().GetProperties();

foreach (PropertyInfo p in propFrom)
{
object value = p.GetValue(FromAddress, new object[] { });
//propTo.SetValue(value, 1);

}


//using (InsuranceEntities ctx = new InsuranceEntities())
//{
// var qry = from efcover in ctx.BeneficiaryItem.Include("Beneficiary").Include("Beneficiary.Cover").Include("BeneficiaryTable")
// where efcover.Beneficiary.Cover.ID == new Guid("") && (efcover.RowStatus == 600 || efcover.RowStatus == 900) &&
// efcover.Beneficiary.OrderInsuredIn == 1
// select efcover;

// var qry1 = from p in ctx.BeneficiaryItem.Include("Beneficiary")
// where p.Beneficiary.Cover.ID == new Guid("") select p;

//}

//using (SapeliPrintEntities ctx = new SapeliPrintEntities())
//{
// var qry = from p in ctx.CaseType.Include("Case")
// where p.Case
//}

//using (SapeliPrintEntities insurancePrint = new SapeliPrintEntities())
//{
// CaseType casetype;


//casetype = (from p in insurancePrint.CaseType.Include("Case")
// where p.ID == new Guid("F471F3AA-6804-4871-9B5F-88237234BF42")
// && p.Case.SelectMany( o => o.CaseCreated.Value.Day < 10)
// select p).FirstOrDefault();

//if (casetype != null)
//{
// //casetype.Case.Where(o => o.CaseCreated.Value.Day < 10);
// //casetype.Case.Load();

// //casetype.Case.Attach( casetype.Case.CreateSourceQuery().Where(o=> o.CaseCreated.Value.Day < 10 ));

// //casetype.CoverItem.Attach(cover.CoverItem.CreateSourceQuery().Where(o => o.RowStatus == 600));
//}


//var roles = from role in ctx.InsuranceRole.Include("InsuranceRoleItem")
// where role.RowStatus == 600 &&
// role.fk_Covers == coverid
// select role;

//foreach (InsuranceRole role in roles)
//{
// role.RowStatus = 900;
// foreach (InsuranceRoleItem item in role.InsuranceRoleItem.Where(o => o.RowStatus == 600))
// {
// item.RowStatus = 900;
// }
//}

//using (Sapeli_InvoiceEntities ctx = new Sapeli_InvoiceEntities())
//{
// IEnumerable invoices = ctx.CreateQuery(query).ToList().DefaultIfEmpty();
// return invoices.ToList();
//}

//var invoice = ctx.Invoice.Include("InvoiceAmount")
// .Where(i => i.InsuranceNo == insuranceNo)
// .Where(i => i.InvoiceStartDate < searchDate)
// .Where(i => i.InvoiceEndDate > searchDate).ToList().DefaultIfEmpty();


//var qry = insurancePrint.CaseType.Where(p => p.CaseCode != "");

//IEnumerable
//objCaseCollection = from p in insurancePrint.CaseType.Include("Case") //.Include("CaseType")
// select p;
//foreach (CaseType objCase in objCaseCollection)
//{

//}

//var qry1 = (from p in insurancePrint.CaseType select p).FirstOrDefault();
//qry1.Case.Load();
//foreach (CaseType obj in objCaseCollection)
//{

//}

//// IEnumerable results = ctx.CreateQuery ("SELECT * FROM [Sapeli_Invoice].[dbo].[Invoice]");

// var qry = from p in insurancePrint.CaseType select p;

// //IEnumerable objFilter = objCaseCollection.Where(p => p.CaseCode == "703");



// //IEnumerable
// //objCaseCollection = from p in insurancePrint.MasterInsurance.Include("InsuranceRole").Include("InsuranceRole.InsuranceRoleItem").Include("InsuranceRole.InsuranceRoleContactInfo")
// // select p;

// foreach (CaseType objCase in objCaseCollection)
//{

//}
//}
}

LINQ terminology

Lazy Loading (or) Deferred Loading
----------------------------------

Refer to : http://dotnetslackers.com/articles/csharp/Load-Lazy-in-LINQ-to-SQL.aspx

Lazy loading indicates the DAL’s ability to load only a portion of the object graph based on some rules. When you add this capability to your DAL, you have implemented the “Lazy Load” pattern (LL).

Lazy loading describes the behavior of an object that doesn’t hold all the data it needs. The object, though, does know how to retrieve missing data on demand. When you load an instance of, say, a Customer entity in a lazy loading model only essential data is loaded—for example, only personal information. Later, when some piece of code needs to go through orders associated with the customer, missing information is located and loaded.


How to disable lazy loading:
----------------------------

NorthwindDataContext ctx = new NorthwindDataContext();
ctx.DeferredLoadingEnabled = false;


Advantage of O/RM (object/relational mapper) tools :
-------------------------

O/RM tools are powerful instruments that save developers a lot of work and are becoming a necessary tool every day. However, you cannot blindly delegate tools the generation of the database code. Make sure you cross-check any T-SQL code and use the SQL profiler extensively during development. If you don’t like the database code being generated, change it. Sometimes you can do better by simply tweaking the O/RM configuration; sometimes you need to manually rewrite the T-SQL code.

Refer to : http://blogs.msdn.com/gblock/archive/2006/10/26/ten-advantages-of-an-orm.aspx


Ten advantages of an ORM (Object Relational Mapper) :
-----------------------------------------------------

1. Facilitates implementing the Domain Model pattern (Thanks Udi). This one reason supercedes all others. In short using this pattern means that you model entities based on real business concepts rather than based on your database structure. ORM tools provide this functionality through mapping between the logical business model and the physical storage model.

2. Huge reduction in code. ORM tools provide a host of services thereby allowing developers to focus on the business logic of the application rather than repetitive CRUD (Create Read Update Delete) logic.

3. Changes to the object model are made in one place. One you update your object definitions, the ORM will automatically use the updated structure for retrievals and updates. There are no SQL Update, Delete and Insert statements strewn throughout different layers of the application that need modification.

4. Rich query capability. ORM tools provide an object oriented query language. This allows application developers to focus on the object model and not to have to be concerned with the database structure or SQL semantics. The ORM tool itself will translate the query language into the appropriate syntax for the database.

5. Navigation. You can navigate object relationships transparently. Related objects are automatically loaded as needed. For example if you load a PO and you want to access it's Customer, you can simply access PO.Customer and the ORM will take care of loading the data for you without any effort on your part.

6. Data loads are completely configurable allowing you to load the data appropriate for each scenario. For example in one scenario you might want to load a list of POs without any of it's child / related objects, while in other scenarious you can specify to load a PO, with all it's child LineItems, etc.

7. Concurrency support. Support for multiple users updating the same data simultaneously.

8. Cache managment. Entities are cached in memory thereby reducing load on the database.

9. Transaction management and Isolation. All object changes occur scoped to a transaction. The entire transaction can either be committed or rolled back. Multiple transactions can be active in memory in the same time, and each transactions changes are isolated form on another.

10. Key Management. Identifiers and surrogate keys are automatically propogated and managed.


If your developing short lifespan applications then it really doesn't matter.

If you're developing long standing enterprise level applications there should be a separation of concerns. The business model should not be running CRUD operations. There should also be persistence ignorance which means loose coupling between ui/model and data access. Why? What happens when MS decides to come out with the next grand data access framework in 4-5 years and linq to sql becomes deprecated? You have to rewrite your most of your app just to use the new framework. If you google the underlined phrases you'll find a lot of information on these subjects.