Here is a simple code snippet using Entity framework:
using (AdventureWorksEntities AwContext = new AdventureWorksEntities())
{
var LoginId = AwContext.Employees
.Where ( u => u.LoginID.Length > 0 )
.Select(u => u.LoginID)
.Take(10);
As you can see from the above code, I am trying to get the top 10 rows from Employee table and only one column LoginID, where loginID length is more than zero. If you fire this query you may get a SQL in SQL Profiler as follows:
SELECT TOP (10)
[c].[LoginID] AS [LoginID]
FROM [HumanResources].[Employee] AS [c]
WHERE LEN( [c].[LoginID] ) > 0
Now, web is full of examples of how to take advantage of multiple cores and make your query run in parallel by using PLINQ. Just add magic word AsParallel in front of the data source and your code will take advantage of multi core and run in multi thread . But if you are developer who read 5000 words a minute, you may miss the fact that PLINQ applies only to LINQ to objects (i.e. IEnumerable-based sources where lambdas are bound to delegates, not IQueryable-based sources where the lambdas are bound to expressions) and you may add AsParallel in your query thinking that you are using multi core of your CPU and some how your query will become faster. Unfortunately your code may still work, but now it has sever side effects behind the scene.
<pre>using (AdventureWorksEntities AwContext = new AdventureWorksEntities())</pre>
{
var LoginId = AwContext.Employees.AsParallel
.Where ( u => u.LoginID.Length > 0 )
.Select(u => u.LoginID)
.Take(10);
With luck your code may still work, but under the cover many things have changed. First one is your Top 10 selection is gone from the query which goes to SQL. This can be a problem if your table is large with couple of millions rows. Second, now you are getting all the columns. Third, if you are lucky then you may get some exceptions in the code as other threads try to process your rest of statements like ‘Where’ clause and they break up with the Null Reference exception else you will just squander the resources in the false pretext that you have written an efficient code. Here is the SQL generated after adding the ‘AsParallel’ keyword.
SELECT
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[NationalIDNumber] AS [NationalIDNumber],
[Extent1].[ContactID] AS [ContactID],
[Extent1].[LoginID] AS [LoginID],
..all other columns
FROM [HumanResources].[Employee] AS [Extent1]
WHERE LEN( [c].[LoginID] ) > 0
So remember, LINQ-to-SQL and LINQ-to-Entities queries will be executed by the respective databases and query providers, PLINQ does not offer a way to parallelize those queries. However, If you wish to process the results of those queries in memory, including joining the output of many heterogeneous queries, then PLINQ can be quite useful.
There are many different ways you can partition a cube. Here I am talking about – how you can use C# to partition a cube from a DTS package?
Using Microsoft.AnalysisServices Object API, you can travel the whole Hierarchy of objects and completely administer an Analysis Service Instance. From the connection Object you can get to the Server and database instances. And in database you can transverse the Cube and their different measures. Once you get hold of a measure then you can transverse the different partitions of measures and dig deep in to each partition query definitions etc.
Read more…
I wanted to read a blob of xml and then display some attribute and few elements as HTML. With the help of Linq and extension method, the code can be very clean and simple.
The xml I want to read looks like as follows:
<Dictionary>
<W S="Arrogate">
<M>To claim or demand unduly.</M>
<S> accroach, appropriate, assume </S>
<A> appropriate, give, hand over </A>
<E>The teenager arrogated that he must get pocket money.</E>
....
Here is the code to convert this XML into HTML in a very nice manner
static void Main(string[] args)
{
string FileName = AppDomain.CurrentDomain.BaseDirectory + "Lesson1.xml";
var doc = XElement.Load(FileName);
var query = from ele in doc.Elements(@"W")
select ele;
foreach (var v in query)
{
Console.WriteLine(v.ToHTMLReady ());
}
}
The function
ToHTMLReady() is the function which does all the magic. However, there is no ‘ToHTMLReady’ function defined on XElement, so where does this come from? This is one of the beauty of extension method. This function is defined as follows:
public static class MyExtensionMethod
{
public static string ToHTMLReady(this XElement ele)
{
return "<B>WORD: </B>" + (string)ele.Attribute("S") + " <B> Example: </B>" + ele.Element("E").Value;
}
}
using this extension method concept you can do all the heavy lifting manuplating XML, while keeping the main code very clean and simple.