LINQtoSQLdebugWriter
This is a tool to trace out the activity of LINQ to SQL. It is not quite as complete as SQL Server Profiler, but you can use it without needing high permissions on the SQL server database.
http://www.scottgu.com/blogposts/linqquery/SqlServerQueryVisualizer.zip
-already in vs2010 ?
You use it like this:
LINQ shaping or projection
-use anonymous type to project (specify fields)
-use nullable type to allow for items that have no joined item
-use null coalescing operator ?? to specify a default
Table inheritance
If you have a single table that contains mixed records, you can model this cleanly in the dbml file, by drag'n'dropping the table type several times. Rename the extra copies, one for each record type. Delete redundant members. Create an inheritance relationship, and configure its discriminator field, and set default type. Set the base type to be abstract.
Then, in your C# you can use the LINQ OfType() extension method to load by type !
Eager Loading:
By default, LINQ to SQL employs lazy or deferred loading, whereby it only loads data from the database, when it is needed. This means that queries which take data from more than one table, can actually result in several SELECT statements being sent to the database, which obviously is not good for performance.
An example would be, where we are loading in data from SalesOrderHeader and SalesOrderDetails: each SalesOrderHeader has a collection of SalesOrderDetails, and so we could have a SELECT statement at least for every SalesOrderHeader we are loading !
To avoid this, we can use DataLoadOptions, to basically specify that whenever we load type X, we also load in type X.Y. An example here, is that whenever we load in SalesOrderHeader, then we also load in it SalesOrderDetails collection:
more LINQ...
This is a tool to trace out the activity of LINQ to SQL. It is not quite as complete as SQL Server Profiler, but you can use it without needing high permissions on the SQL server database.
http://www.scottgu.com/blogposts/linqquery/SqlServerQueryVisualizer.zip
-already in vs2010 ?
You use it like this:
MyDatabaseContext ctx = new MyDatabaseContext();
LINQtoSQLdebugWriter writer = new LINQtoSQLdebugWriter();
ctx.Log = writer;
LINQ shaping or projection
-use anonymous type to project (specify fields)
-use nullable type to allow for items that have no joined item
-use null coalescing operator ?? to specify a default
var products = from p in dc.Products
select new
{
ProductName = p.Name,
Revenue = string.Format({"0:c"},
Sum( o => (decimal?)o.OrderQty * (decimal?)o.UnitPrice ) ?? 0
)
}
Table inheritance
If you have a single table that contains mixed records, you can model this cleanly in the dbml file, by drag'n'dropping the table type several times. Rename the extra copies, one for each record type. Delete redundant members. Create an inheritance relationship, and configure its discriminator field, and set default type. Set the base type to be abstract.
Then, in your C# you can use the LINQ OfType() extension method to load by type !
Eager Loading:
By default, LINQ to SQL employs lazy or deferred loading, whereby it only loads data from the database, when it is needed. This means that queries which take data from more than one table, can actually result in several SELECT statements being sent to the database, which obviously is not good for performance.
An example would be, where we are loading in data from SalesOrderHeader and SalesOrderDetails: each SalesOrderHeader has a collection of SalesOrderDetails, and so we could have a SELECT statement at least for every SalesOrderHeader we are loading !
To avoid this, we can use DataLoadOptions, to basically specify that whenever we load type X, we also load in type X.Y. An example here, is that whenever we load in SalesOrderHeader, then we also load in it SalesOrderDetails collection:
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith(c => c.SalesOrderDetails);
ctx.LoadOptions = dlo;
more LINQ...
Comments
Post a Comment