<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>linq &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://wordpress.com/tag/linq/</link>
	<description>Feed of posts on WordPress.com tagged "linq"</description>
	<pubDate>Sun, 12 Oct 2008 21:29:49 +0000</pubDate>

	<generator>http://wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[WPF Elliptical Layout Control - 3D!]]></title>
<link>http://iltc.wordpress.com/?p=54</link>
<pubDate>Sat, 11 Oct 2008 21:40:08 +0000</pubDate>
<dc:creator>ilovetocode</dc:creator>
<guid>http://iltc.it.wordpress.com/2008/10/11/wpf-elliptical-layout-control-3d/</guid>
<description><![CDATA[After finishing my last post, WPF Elliptical Layout Control, I sat down and wondered what to do ne]]></description>
<content:encoded><![CDATA[<p>After finishing my last post, <a title="Read WPF Elliptical Layout Control" rel="bookmark" href="http://iltc.wordpress.com/2008/10/08/wpf-elliptical-layout-control/">WPF Elliptical Layout Control</a>, I sat down and wondered what to do next. It occurred to me that creating a 3D carousel in WPF is a common question and one that doesn't have all that many examples. A 3D control is also a natural progression from the 2D control and is not all that different. All we need to do is layout the objects in 2D, then rotate those points according to the orientation of the imaginary layout ellipse, taking in to account depth.</p>
<p>This example creates a new control which derives from <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.aspx">FrameworkElement </a>and is very similar to the implementation of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.viewport3d.aspx">Viewport3D</a> control. The new control overrides the measure and layout methods of FrameworkElement and positions the child controls (supplied as a collection of UIElements) according to the orientation of an imaginary 2D ellipse in 3D space.</p>
<p>It is assumed that the reader has some knowledge of custom controls, <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx">LINQ </a>and <a href="http://msdn.microsoft.com/en-us/library/ms747437.aspx">3D </a>in WPF. The code provided is by no means an ideal implementation of the theory, it should be thought of more as a quick demonstration of the concept. And remember, in WPF there is almost always more than one way of doing the same thing, mine is just my take on the problem :0)</p>
[caption id="attachment_55" align="aligncenter" width="510" caption="3D Elliptical Layout Panel Demo"]<a href="http://iltc.files.wordpress.com/2008/10/2008-10-11_ellipticallayoutpanel3d_screenshot.png"><img class="size-large wp-image-55" title="2008-10-11_ellipticallayoutpanel3d_screenshot" src="http://iltc.wordpress.com/files/2008/10/2008-10-11_ellipticallayoutpanel3d_screenshot.png?w=510" alt="3D Elliptical Layout Panel Demo" width="510" height="346" /></a>[/caption]
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2filtc.wordpress.com%2f2008%2f10%2f11%2fwpf-elliptical-layout-control-3d%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2filtc.wordpress.com%2f2008%2f10%2f11%2fwpf-elliptical-layout-control-3d%2f&#38;border=F78B0C&#38;fgcolor=818181&#38;bgcolor=FFFFFF&#38;cfgcolor=FFFFFF&#38;cbgcolor=818181" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<p><!--more--></p>
<p><strong>The Idea</strong></p>
<p>In the last example, we worked out how to lay out controls as if they were positioned at equal intervals around the edge of an imaginary ellipse. We now want to take that ellipse from a 2D space in to 3D, giving a sense of depth in the control.</p>
<p>Taking the existing layout logic in to the third dimension is fairly trivial if we remember that the positions we calculate for child controls in the 2D version could be thought of as being 3D points, all at the same distance away from the screen, i.e. z = 0. So by treating the points that are generated as 3D points, each with a value of zero for z (their depth), we can then rotate them to match the orientation of the layout ellipse (defined by an x, y and z axis rotation).</p>
<p>In short, we calculate the position of each child element as before, then rotate that point in 3D to match the rotation of a virtual layout ellipse. The layout ellipse will be configured as before, although this time we need to allow a way to configure its orientation. This is as simple as adding three new properties to allow an angle of rotation to be specified for each axis.</p>
<p>When it comes to the control, we need to look to 3D controls in WPF. We could create our own control which lays out its content, then projects it in to 3D and renders the control scaled such that they appear to be 3D, but that's no fun and way too much effort! It would somehow be great if we could take advantage of the relatively new <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.viewport2dvisual3d.aspx">Viewport2DVisual3D</a> control, effictively a 3D wrapper for a <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.aspx">UIElement</a>. It would also be great if the control exposed just a collection of UIElements, which internally it could wrap in a Viewport2DVisual3D control. </p>
<p>Now we know what our items are doing, we need to think of a suitable container. If we look at the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.viewport3d.aspx">Viewport3D</a> control, we can see that it wraps a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.viewport3dvisual.aspx">Viewport3DVisual</a>, exposing a collection of UIElement objects which it implicitly converts to <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.visual3d.aspx">Visual3D </a>objects and adds to the inner Viewport3DVisual, as well as one or two other things. This is very similar to what we want to do, with the difference that we want to also define the layout mechanism too.</p>
<p><strong>The Theory</strong></p>
<p>I guess the first thing to tackle here is how to take the generated 2D position for each child control and turn that in to a 3D point, which we can use to create a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.translatetransform3d.aspx">TranslateTransform3D</a>. For each point we generate on the ellipse, create a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.point3d.aspx">Point3D</a> object using the x and y values of the 2D point and supply a z value of 0d (or whatever you want for the ellipse's distance from the image plane).</p>
<p>Next, rotate the point around each axis using the rotations used to describe the layout ellipse's orientation.  If you're familiar with 3D graphics you may well be aware of <a href="http://en.wikipedia.org/wiki/Gimbal_lock">Gimbal lock</a>, something we want to avoid when we're rotating the child control positions. To combat this, the rotation method will use quaternions. If you've not come across quaternions before, I recommend a quick look on <a href="http://www.google.co.uk/search?hl=en&#38;q=quaternion">Google</a> as there are far better explanations out there than I can give. Also look at <a href="http://www.genesis3d.com/~kdtop/Quaternions-UsingToRepresentRotation.htm">this</a> site which gives the theory behind the math used to rotate a point.</p>
<p>[sourcecode language='csharp']<br />
private readonly static Vector3D UnitXAxis3D = new Vector3D(1d, 0d, 0d);<br />
private readonly static Vector3D UnitYAxis3D = new Vector3D(0d, 1d, 0d);<br />
private readonly static Vector3D UnitZAxis3D = new Vector3D(0d, 0d, 1d);</p>
<p>private static Point3D RotatePoint3D(Point3D point, double xRotation, double yRotation, double zRotation)<br />
{<br />
    Quaternion xQ = new Quaternion(UnitXAxis3D, xRotation);<br />
    Quaternion yQ = new Quaternion(UnitYAxis3D, yRotation);<br />
    Quaternion zQ = new Quaternion(UnitZAxis3D, zRotation);</p>
<p>    Quaternion xyzQ = xQ * yQ * zQ;<br />
    Quaternion xyzQc = xyzQ;<br />
    xyzQc.Conjugate();</p>
<p>    Quaternion pQ = new Quaternion(point.X, point.Y, point.Z, 0d);<br />
    Quaternion q = xyzQ * pQ * xyzQc;<br />
    Point3D rotatedPoint = new Point3D(q.X, q.Y, q.Z);</p>
<p>    return rotatedPoint;<br />
 }<br />
[/sourcecode]</p>
<p>With the Point3D in hand, a TranslateTransform3D object can be created and applied to the child Viewport2DVisual3D control. This leads to the next question of how we are going to handle the wrapping of UIElements. With the addition of the Viewport2DVisual3D control, it is now possible to easily use a UIElement in a 3D scene, maintaining all the usual input handling, so no more messing around with visual brushes and models! You don't get off too lightly though as you still need to provide the Viewport2DVisual3D with some information. This includes a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.geometry3d.aspx">Geometry3D</a> object detailing a mesh that defines the surface which the inner <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx">Visual</a> is to be rendered on; the material which should be used when rendering the Visual on the mesh and finally the Visual object itself.</p>
<p>To keep things simple, we can define a basic mesh containing a unit square. This mesh is going to be used as the geometry for each control surface, so we need a way of sizing it according to the dimensions of the control being rendered. This is again a trivial problem and is solved by creating a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.scaletransform3d.aspx">ScaleTransform3D</a> object which scales the Viewport2DVisual3D control so that it is scaled by the dimensions of the inner Visual object, ignoring z. Nice!</p>
<p>The scale transform is created for each Viewport2DVisual3D control each time the MeasureOverride method is called. There is no particular reason to place this code here, other than it keeps the sizing logic separate from the layout logic, keeping things a bit better organised. The next code snippet is included to highlight the use of the <a href="http://msdn.microsoft.com/en-us/library/bb360913.aspx">OfType&#60;T&#62;()</a> LINQ extension method, used on the Children property of the control's inner Viewport3DVisual. This method returns an IEnumerable&#60;T&#62; object containing all the items in the collection with a type specified by T. Because we know that our Children collection contains a light somewhere within it, we want to skip over that when scaling the child objects.</p>
<p>[sourcecode language='csharp']<br />
protected override Size MeasureOverride(Size availableSize)<br />
{<br />
    //Iterate all the children of the inner viewport.<br />
    foreach (Viewport2DVisual3D visualChild <br />
        in viewport3DVisual.Children.OfType<Viewport2DVisual3D>())<br />
    {<br />
        //Get the inner UIElement<br />
        UIElement element = visualChild.Visual as UIElement;<br />
        //Create a scale transform so that the control appears the right size<br />
        ScaleTransform3D scaleTransform<br />
            = new ScaleTransform3D(element.DesiredSize.Width, element.DesiredSize.Height, 0d);</p>
<p>        //Add the scale transform in to the Viewport2DVisual3D's transform group.<br />
        AssertTransform3D<ScaleTransform3D>(scaleTransform, visualChild);<br />
    }</p>
<p>    return availableSize;<br />
}<br />
[/sourcecode]</p>
<p>The container that will hold all of these Viewport2DVisual3D controls will need to be a Viewport3DVisual. Again because we are dealing with 3D we need to define a few extra things for the Viewport3DVisual. The first is a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.camera.aspx">Camera</a> object which defines how controls displayed in the Viewport3DVisual appear to the viewer. The second is a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.light.aspx">Light</a> object which defines an <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.ambientlight.aspx">AmbientLight</a> object used to provide a uniform white light in the 3D scene (otherwise everything would be dark and you'd see nothing more than a black screen).</p>
<p>To make the controls appear to be positioned within 3-dimensional space, we will use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.perspectivecamera.aspx">PerspectiveCamera</a>, meaning objects which are placed deeper in to the control (further from the screen) will appear smaller than those which are closer to the screen. All we need to do to configure the camera is tell it where it is and the direction it is looking. Because we are keeping things simple here, we'll position our camera somewhere along the positive z-axis, and have it looking down the z-axis (towards the origin). This means our camera will be looking straight on at the objects placed within the control, with the layout ellipse's origin at the origin of the scene.</p>
<p><strong>The Solution</strong></p>
<p>Here I present a simple demonstration of the above in the attached solution. The solution contains a simple Window which hosts the custom 3D ellipse layout control. The ellipse layout control is included in the ElliptiseLayout3DPanel class and adds several dependency properties and a few methods to the FrameworkElement class.</p>
[caption id="attachment_67" align="alignright" width="127" caption="EllipticalLayoutPanel3D Class Diagram"]<a href="http://iltc.files.wordpress.com/2008/10/2008-10-11_ellipticallayoutpanel3d_classdiagram1.png"><img class="size-full wp-image-67     " title="2008-10-11_ellipticallayoutpanel3d_classdiagram1" src="http://iltc.wordpress.com/files/2008/10/2008-10-11_ellipticallayoutpanel3d_classdiagram1.png" alt="EllipticalLayoutPanel3D Class Diagram" width="127" height="331" /></a>[/caption]
<p>The main window also contains a few controls which allow you to interact with the layout control, allowing you to specify the size, location and pose of the layout control as well as add or remove items to the control. There are also two buttons will moves the controls around the ellipse, one at a time. This is a bit of a jig and is as simple as removing an item from the top of the collection held in the control's Children property and adding it to the end of the collection. The reverse is true for moving back through the items. The light sometimes gets caught up in this, so you may not see anything happen for a click or two when switching direction.</p>
<p>With the default orientation of the ellipse when the demo app first runs, changes made to the z-rotation slider will have the effect of spinning the controls around, in the fashion of a carousel. This could be used to animate the rotation items in the control.</p>
<p>To the right is a class diagram, showing the structure of the layout control. You can see it’s pretty simple and doesn’t need to add much to its FrameworkElement ancestor. </p>
<p>Below is a screen-shot of the demo application in action. The values of the sliders are directly bound to the panel’s dependency properties, meaning the only code-behind is for the four button event handlers.</p>
<p> </p>
[caption id="attachment_55" align="aligncenter" width="510" caption="3D Elliptical Layout Panel Demo"]<a href="http://iltc.files.wordpress.com/2008/10/2008-10-11_ellipticallayoutpanel3d_screenshot.png"><img class="size-large wp-image-55" title="2008-10-11_ellipticallayoutpanel3d_screenshot" src="http://iltc.wordpress.com/files/2008/10/2008-10-11_ellipticallayoutpanel3d_screenshot.png?w=510" alt="3D Elliptical Layout Panel Demo" width="510" height="346" /></a>[/caption]
<p>The code is available for <a href="http://ilovetocode.googlecode.com/files/EllipticalLayoutControl3DDemo_VS2008.zip">download</a> as a Visual Studio 2008 (SP1) solution, built against .Net 3.5 SP1.</p>
<p><strong>Going Forwards</strong></p>
<p>The light source and other properties of the control are not configurable. You may want to expose the lights as a separate collection, or at least allow the colour to be altered.</p>
<p>The control will need some more work to enable it to be used in an ItemsPanelTemplate, so it won't work out of the box.</p>
<p>You may want to add proper methods for controlling the order of items in the control, possibly animating this.</p>
<p>All and any comments / bugs / suggestions are welcomed!</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2filtc.wordpress.com%2f2008%2f10%2f11%2fwpf-elliptical-layout-control-3d%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2filtc.wordpress.com%2f2008%2f10%2f11%2fwpf-elliptical-layout-control-3d%2f&#38;border=F78B0C&#38;fgcolor=818181&#38;bgcolor=FFFFFF&#38;cfgcolor=FFFFFF&#38;cbgcolor=818181" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Dealing with REST Web Services]]></title>
<link>http://aspguy.wordpress.com/?p=123</link>
<pubDate>Fri, 10 Oct 2008 18:22:52 +0000</pubDate>
<dc:creator>Aref Karimi</dc:creator>
<guid>http://aspguy.it.wordpress.com/2008/10/10/dealing-with-rest-web-services/</guid>
<description><![CDATA[
At the time of writing this post, .NET has no specific facility to work with REST services. Though,]]></description>
<content:encoded><![CDATA[<p style="text-align:center;"><a href="http://secure.hostgator.com/cgi-bin/affiliates/clickthru.cgi?id=lollypop55" target="_blank"><img class="aligncenter" src="http://www.hostgator.com/affiliates/banners/hgator-468x60d.gif" alt="" /></a></p>
<p style="text-align:justify;">At the time of writing this post, .NET has no specific facility to work with REST services. Though, some services over the web provide information in REST format. For example, <a href="http://www.geonames.org" target="_blank">http://www.geonames.org</a> (that provides gegraphic information) has a lot of free web services to get geographical information. The other day, I needed to use these services to let the end user choose his/her country and then state from two drop-down lists. So I had to figure out how to consume such services. In this post we will use the following service as an case-study:</p>
<p><a href="http://ws.geonames.org/countryInfo?lang=it&#38;country=DE" target="_blank">http://ws.geonames.org/countryInfo?lang=it&#38;country=DE</a></p>
<p>If you navigate to this link you will get the following result:</p>
<p><a href="http://aspguy.files.wordpress.com/2008/10/rest.jpg"><img class="alignnone size-full wp-image-124" title="rest" src="http://aspguy.wordpress.com/files/2008/10/rest.jpg" alt="" width="422" height="414" /></a></p>
<p style="text-align:justify;">As is seen, the resulting XML is pretty straight forward.  With knowing that REST web services work with GET and POST methods, it is nearly nothing except working with xml!</p>
<p style="text-align:justify;">For example, to query a REST service, like the one I mentioned above, we can use LINQ's XAML functionality.  The XElement class (declared in System.Linq.XML) provides a Load method that can retrieve an XML data from a url with GET  method:</p>
<p><span style="color:#800000;">XElement rootXml = XElement.Load("http://ws.geonames.org/countryInfo?lang=it&#38;country=DE");</span></p>
<p><span style="color:#800000;">var Countries = from C in rootXml.Elements()<br />
select new { Code = (string)C.Element("countryCode"),<br />
Name = (string)C.Element("countryName") };<br />
foreach (var x in Countries)<br />
Console.WriteLine("{0}  {1}", x.Code, x.Name);<br />
Console.ReadKey();</span></p>
<p>After retrieveing the full xml, you can extract any kind of information you want. no matter how complex is it!</p>
<p style="text-align:justify;">Adding or modifying information using POST method is a bit more complex. First we have to obtain a channel by which we may POST the information. Then we have to create an instance of data entity in XML format. Finally we have to POST data through the channel.</p>
<p style="text-align:justify;">To get access to REST service we use a HttpWebRequest since it let's use the POST method and let us specify the type of information we are posting.</p>
<p><span style="color:#800000;">HttpWebRequest channel = (HttpWebRequest)WebRequest.Create("http://ws.geonames.org/countryInfo");<br />
channel.Method = "POST";<br />
channel.ContentType = "text/xml";</span></p>
<p>HttpWebRequest class provides a stream to which we can write information :</p>
<p><span style="color:#800000;">StreamWriter sw = new StreamWriter(channel.GetRequestStream());</span></p>
<p>Then we can create a concerete entity to post:</p>
<p><span style="color:#800000;">XElement country = new XElement("country" , </span></p>
<p><span style="color:#800000;">new XElement("countryCode", "AUS"),</span></p>
<p><span style="color:#800000;">new XElement("countryName" , "Australia"),</span></p>
<p><span style="color:#800000;">....</span></p>
<p><span style="color:#800000;">);</span></p>
<p>Finally we just need to save this XElement object to the stream we obtained from HttpWebRequest:</p>
<p><span style="color:#800000;">country.Save(sw);</span></p>
<p style="text-align:center;">This post shows how to deal with REST web services, or any other resource that let us work it in a POST/GET manner. Thanks to Linq to XML working with such services is now too easy.<br />
<a href="http://secure.hostgator.com/cgi-bin/affiliates/clickthru.cgi?id=lollypop55" target="_blank"><img class="aligncenter" src="http://www.hostgator.com/affiliates/banners/hgator-468x60d.gif" alt="" /></a></p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Linq4SP - RC1 Available]]></title>
<link>http://koenvosters.wordpress.com/?p=139</link>
<pubDate>Fri, 10 Oct 2008 15:09:08 +0000</pubDate>
<dc:creator>koenvosters</dc:creator>
<guid>http://koenvosters.it.wordpress.com/2008/10/10/linq4sp-rc1-available/</guid>
<description><![CDATA[Dotneteers reports that Release Candidate 1 of Linq4SP has been released. What is it? Linq4SP is a r]]></description>
<content:encoded><![CDATA[<p><a href="http://dotneteers.net/blogs/aghy/archive/2008/10/08/linq4sp-rc1-is-available-to-download.aspx">Dotneteers </a>reports that Release Candidate 1 of Linq4SP has been released. What is it? Linq4SP is a readable-writable Linq provider to SharePoint (WSS 3.0 and MOSS 2007), with a lot of interesting and useful features: support the handling of list items, documents, folders, content types, enumerations usable for choice columns, ... Basically it allows you to use LINQ on SharePoint objects. I haven't tried it out yet, probably will this weekend, but it looks promising. Get it <a href="http://www.lmsolutions.hu/linq4sp">here</a>.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ To SharePoint o LINQ4SP: Disponible la RC1!]]></title>
<link>http://jcgonzalezmartin.wordpress.com/2008/10/09/linq-to-sharepoint-o-linq4sp-disponible-la-rc1/</link>
<pubDate>Thu, 09 Oct 2008 22:36:42 +0000</pubDate>
<dc:creator>Juan Carlos González Martín</dc:creator>
<guid>http://jcgonzalezmartin.it.wordpress.com/2008/10/09/linq-to-sharepoint-o-linq4sp-disponible-la-rc1/</guid>
<description><![CDATA[Por fin, después de unos meses en lo que poco se ha sabido sobre la evolución de esta extensión d]]></description>
<content:encoded><![CDATA[<p>Por fin, después de unos meses en lo que poco se ha sabido sobre la evolución de esta extensión de LINQ específicamente pensada para realizar consultas contra elementos de SharePoint, tenemos <a href="http://dotneteers.net/blogs/aghy/archive/2008/10/08/linq4sp-rc1-is-available-to-download.aspx">disponible la primera release candidate (RC1) de LINQ To SharePoint (LINQ4SP)</a>. Podéis descargaros LINQ4SP de <a href="http://www.lmsolutions.hu/linq4sp">este enlace</a>.</p>
<p align="center"><a href="http://www.lmsolutions.hu/linq4sp"><img height="116" alt="image" src="http://dotneteers.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/aghy/image3.png" width="244" border="0"></a></p>
<p align="left">Aparte de la noticia de la RC1 de LINQ4SP, creo que también es destacable que esta extensión de LINQ para SharePoint <a href="http://www.codeplex.com/LINQtoSharePoint">no esté incluida en el proyecto original de Codeplex</a>, lo que creo que va en la dirección de que la versión final de LINQ4SP será de pago...veremos que pasa en el futuro. En cualquier caso, esta RC1 es una excelente oportunidad de probar las prestaciones de LINQ4SP.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ]]></title>
<link>http://unanue.wordpress.com/?p=3</link>
<pubDate>Thu, 09 Oct 2008 19:13:20 +0000</pubDate>
<dc:creator>unanue</dc:creator>
<guid>http://unanue.it.wordpress.com/2008/10/09/linq/</guid>
<description><![CDATA[
]]></description>
<content:encoded><![CDATA[<br />
]]></content:encoded>
</item>
<item>
<title><![CDATA[Starting to LINQ]]></title>
<link>http://idodotnet.wordpress.com/?p=18</link>
<pubDate>Thu, 09 Oct 2008 11:45:40 +0000</pubDate>
<dc:creator>smakazmi</dc:creator>
<guid>http://idodotnet.it.wordpress.com/2008/10/09/starting-to-linq/</guid>
<description><![CDATA[Hello and welcome to the first in a series of posts in which I’ll be exploring LINQ.
As I’ve pre]]></description>
<content:encoded><![CDATA[<p>Hello and welcome to the first in a series of posts in which I’ll be exploring LINQ.<br />
As I’ve previously mentioned that these posts are made so that I and other people visiting this blog can refer to them quickly in time of need, I’ll try to keep the posts as clear and concise as possible.</p>
<p><strong>Introduction</strong></p>
<p>LINQ (short for Language INtegrated Query) is an extension to .NET languages. It allows us developers to query different sources of data.</p>
<p><strong>Why do we need it?</strong></p>
<p>In enterprise application development, we frequently face the need to query data. The data could be present in</p>
<ul>
<li>CSV</li>
<li>XML</li>
<li>.NET Collections</li>
<li>SQL Server, Oracle, MySQL, PostgreSQL and the list goes on</li>
</ul>
<p>For each of these data sources we need to resort to a querying API that is specific to the data source. For example XPath, XQuery, T-SQL, PL/SQL</p>
<p>LINQ aims to facilitate .NET developers by providing a querying API that is baked right into our favorite programming languages (i.e. C#, VB etc.) and can be extended to provide querying support to any data source. This provides a number of benefits:</p>
<ul>
<li>Reduced learning curve</li>
<li>Reduced training costs</li>
<li>Reduced risk due to type checking at compile time</li>
<li>Less amount of rework in case of switching back-end data source</li>
</ul>
<p>LINQ can be extended by using a provider pattern. Currently the following providers are available out of the box.</p>
<ul>
<li>LINQ to SQL</li>
<li>LINQ to DataSet</li>
<li>LINQ to Objects</li>
<li>LINQ to Xml</li>
<li>LINQ to Entities</li>
</ul>
<p>We’ll have a look at each of these individually in forthcoming posts.</p>
<p><strong>How to LINQ?</strong></p>
<p>Using LINQ effectively requires us to learn some other new features that LINQ is built upon and that are also introduced in .NET 3.0. These include:</p>
<ul>
<li>Anonymous types</li>
<li>Lambdas</li>
<li>Object Initializer</li>
<li>Extension Methods</li>
</ul>
<p>In the next post I’ll cover these features and we’ll further explore LINQ in the following posts.</p>
<p>Bye for now</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[ASP.NET Dynamic Data]]></title>
<link>http://namdh.wordpress.com/?p=369</link>
<pubDate>Thu, 09 Oct 2008 06:53:12 +0000</pubDate>
<dc:creator>namdh</dc:creator>
<guid>http://namdh.it.wordpress.com/2008/10/09/aspnet-dynamic-data/</guid>
<description><![CDATA[Bài viết này được dịch lại từ http://mattberseth.com/blog/2008/08/aspnet_dynamic_data]]></description>
<content:encoded><![CDATA[<p>Bài viết này được dịch lại từ <a href="http://mattberseth.com/blog/2008/08/aspnet_dynamic_data_simple_5_t.html">http://mattberseth.com/blog/2008/08/aspnet_dynamic_data_simple_5_t.html</a>, bài viết giới thiệu một kiểu ASP.NET web application mới được đưa ra từ bản .NET 3.5 SP1: Dynamic Data Web Application.</p>
<p><!--more--></p>
<p>Tôi đã khắc khoải chờ đợi ngày <a href="http://msdn.microsoft.com/en-us/library/cc488545.aspx">Dynamic Data</a> được phát hành. Và giờ thì nó đây (đã được phát hành cùng với VS 2008 và .NET 3.5 SP1) Tôi quyết định thử xem nó có gì bằng cách thử tạo một ứng dụng web Dynamic Data cho phép bạn duyệt qua 5 bảng chính có trong CSDL Northwind: Customers, Employees, Orders, Products and Suppliers. Hãy đọc một cách chi tiết và dừng quên <a href="http://mattberseth2.com/downloads/intro_dynamic_data.zip">download mã nguồn</a>.  Dịch vụ <a href="http://www.google.com/aclk?sa=L&#38;ai=B4MfCWqWtSOeWK4e4hQT8vpjbA-ixpyjY08LJBbK2vrwC8Iw1CAAQARgBIKfGnQY4AFCbnI61_P____8BYMm-pofco8gRyAEByAKUnMUB2QMKN3FZpQVUBQ&#38;sig=AGiWqty_MyNZSkWmmbWjJpvmLH7tlSauyQ&#38;q=http://www.discountasp.net/go/go.aspx%3Fi%3D958">DiscountASP</a> hiện chưa hoàn toàn nâng cấp lên SP1 nên tôi không thể cài một bản demo cho chương trình này. Hi vọng là họ sẽ nâng cấp sớm, nhưng tôi cũng đảm bảo là sẽ có nhiều ảnh chụp màn hình để bạn có thể xem và thấy được nó thế nào.</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_eda76111-276c-4391-afd7-2abd8f5ec4c0.png" alt="image" width="759" height="359" /></p>
<h4>Dynamic Data là gì?</h4>
<p>Tôi luôn theo sát ASP.NET, nhưng tôi không biết là Dynamic Data đã được đưa vào bản SP1. Tôi hỏi một số đồng nghiệp và họ cũng tỏ vẻ ngạc nhiên. Và không ít người thậm chí chưa từng nghe về Dynamic Data. Nếu bạn rơi vào trường hợp này, thì vài đoạn trích dẫn dau đây sẽ mô tả Dynamic Data là gì.</p>
<p>Từ <a href="http://en.wikipedia.org/wiki/ASP.NET_Dynamic_Data">Wikipedia</a> ...</p>
<blockquote><p><em>ASP.NET Dynamic Data is a </em><em>web application</em><em> </em><em>scaffolding</em><em> framework from </em><em>Microsoft</em><em>, shipped as an extension to </em><em>ASP.NET</em><em>, that can be used to build data driven web applications. It exposes tables in a </em><em>database</em><em> by encoding it in the </em><em>URI</em><em> of the </em><em>ASP.NET</em><em> web service, and the data in the table is automatically rendered to </em><em>HTML</em><em>. The process of rendering can be controlled using custom design </em><em>templates</em><em>. Internally, it discovers the </em><em>database schema</em><em> by using the database </em><em>metadata</em><em>. (nguyên gốc)</em></p></blockquote>
<p>From <a href="http://www.asp.net/dynamicdata/">asp.net</a> ...</p>
<blockquote><p><em>ASP.NET Dynamic Data provides a framework that enables you to quickly build a functional data-driven application, based on a LINQ to SQL or Entity Framework data model. It also adds great flexibility and functionality to the <strong>DetailsView</strong>, <strong>FormView</strong>, <strong>GridView</strong>, and <strong>ListView</strong> controls in the form of smart validation and the ability to easily change the display of these controls using templates.(nguyên gốc)</em></p></blockquote>
<p><span style="color:#3366ff;">Nôm na ASP.NET Dynamic Data là một bộ khung cho phép xây dựng các ứng dụng nặng về dữ liệu, dựa trên LINQ to SQL hoặc Entity Framework. Dựa trên cấu trúc của CSDL mà Dynamic Data Framework (DDF)  sẽ tạo nên các trang web cho phép người dùng xem/chèn/xóa/sửa dữ liệu. Ngoài ra, nó còn cho phép tùy biến để người dùng có thể thêm vào các cơ chế kiểm tra tính hợp lệ của dữ liệu, hoặc chỉnh sửa lại các mẫu để thay đổi cách hiển thị dữ liệu - NG.</span></p>
<p>Và với chương trình mẫu này, tôi sẽ dùng Dynamic Data để tạo một ứng dụng web cho phép tôi duyệt qua CSDL Northwind.</p>
<h4>Tạo một website Dynamic Data</h4>
<p>Để bắt đầu, bạn hãy chọn File -&#62; New -&#62; Web Site và chọn <em>Dynamic Data Entities Web Site</em> hay <em>Dynamic Data Web Site. </em>Nếu bạn đang định dùng ADO.NET Entity Framework để truy cập dữ liệu thì chọn cái thứ nhất, ngược lại, nếu dùng LINQ to SQL thì hãy chọn cái thứ hai. Với ví dụ này tôi sẽ dùng LINQ to SQL, do vậy tôi chọn Dynamic Data Web Site.</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_3a38f9c7-f703-42a2-91ef-22442549639d.png" alt="image" width="641" height="418" /></p>
<p>Khi VS đã tạo xong, bạn sẽ thấy một số file/folder được đưa vào trong solution. Trong đó sẽ có một folder có tên là DynamicData, bên trong chứa một số các folder khác, và trong mỗi folder con này sẽ chứa các UserControl và các trang ASP.NET.</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_fcd3dea1-f6cc-43ec-b5dc-c2d6482cd9c6.png" alt="image" width="515" height="757" /></p>
<h4>Tạo và đăng ký LINQ to SQL DataContext</h4>
<p>Tôi dự định dùng LINQ to SQL để truy cập vào CSDL Northwind, vậy nên tôi dùng VS designer để tạo các lớp LINQ to SQL cho các bảng tôi muốn bằng cách kéo các bảng này từ Server Explorer và thả nó lên trên trình thiết kế LINQ to SQL.</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_a8a6f42b-8ce7-4074-b341-89620f66a154.png" alt="image" width="628" height="316" /></p>
<p>Sau khi tạo ra DataContext, tôi cập nhật file Global.asax và đăng ký NorthwindDataContext với hệ thống DynamicData. Website được tạo ra đã chứa sẵn mã lệnh để làm những điều này, bạn chỉ cần đọc các đoạn ghi chú và sửa lại code theo cách bạn muốn. Và sau khi chỉnh sửa, nó sẽ trông như sau:</p>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> RegisterRoutes(RouteCollection routes) {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     MetaModel model = <span style="color:#0000ff;">new</span> MetaModel();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#008000;">//                    IMPORTANT: DATA MODEL REGISTRATION </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#008000;">// Uncomment this line to register LINQ to SQL classes or an ADO.NET Entity Data</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     <span style="color:#008000;">// model for ASP.NET Dynamic Data. Set ScaffoldAllTables = true only if you are sure </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#008000;">// that you want all tables in the data model to support a scaffold (i.e. templates) </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#008000;">// view. To control scaffolding for individual tables, create a partial class for </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     <span style="color:#008000;">// the table and apply the [Scaffold(true)] attribute to the partial class.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     <span style="color:#008000;">// Note: Make sure that you change "YourDataContextType" to the name of the data context</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     <span style="color:#008000;">// class in your application.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     model.RegisterContext(<span style="color:#0000ff;">typeof</span>(NorthwindDataContext), <span style="color:#0000ff;">new</span> ContextConfiguration() { ScaffoldAllTables = <span style="color:#0000ff;">true</span> });</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     <span style="color:#008000;">// The following statement supports separate-page mode, where the List, Detail, Insert, and </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     <span style="color:#008000;">// Update tasks are performed by using separate pages. To enable this mode, uncomment the following </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     <span style="color:#008000;">// route definition, and comment out the route definitions in the combined-page mode section that follows.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     routes.Add(<span style="color:#0000ff;">new</span> DynamicDataRoute(<span style="color:#006080;">"{table}/{action}.aspx"</span>) {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>         Constraints = <span style="color:#0000ff;">new</span> RouteValueDictionary(<span style="color:#0000ff;">new</span> { action = <span style="color:#006080;">"List&#124;Details&#124;Edit&#124;Insert"</span> }),</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>         Model = model</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>     });</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span> </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>     <span style="color:#008000;">// The following statements support combined-page mode, where the List, Detail, Insert, and</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>     <span style="color:#008000;">// Update tasks are performed by using the same page. To enable this mode, uncomment the</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>     <span style="color:#008000;">// following routes and comment out the route definition in the separate-page mode section above.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>     <span style="color:#008000;">//routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>     <span style="color:#008000;">//    Action = PageAction.List,</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>     <span style="color:#008000;">//    ViewName = "ListDetails",</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>     <span style="color:#008000;">//    Model = model</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>     <span style="color:#008000;">//});</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span> </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  31:</span>     <span style="color:#008000;">//routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  32:</span>     <span style="color:#008000;">//    Action = PageAction.Details,</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  33:</span>     <span style="color:#008000;">//    ViewName = "ListDetails",</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  34:</span>     <span style="color:#008000;">//    Model = model</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  35:</span>     <span style="color:#008000;">//});</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  36:</span> }</pre>
</div>
<p>Và sau khi hoàn thành, bạn sẽ có một website trông rất "xinh" và nhiều tính năng. Trên trang chính sẽ hiện ra tất cả các bảng mà bạn có:</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_7808fb7a-0592-44fc-a877-813007996530.png" alt="image" width="718" height="409" /></p>
<p>Và các trang để xem nội dung chứa trong các bảng (cho phép sắp xếp và phân trang):</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_6efa65c3-c072-41bc-bf93-9bc198d5165a.png" alt="image" width="758" height="510" /></p>
<p>Và cả các trang để nhập thêm, cập nhật và xem chi tiết:</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_1db39057-b16c-4b80-832e-b1ef26c64136.png" alt="image" width="757" height="511" /></p>
<h4>Tùy biến</h4>
<p>Nếu tôi đã hài lòng với những gì các trang mặc nhiên có, tôi có thể ngừng tại đây và có một ứng dụng web hỗ trợ đầy đủ các thao tác CRUD (<span style="color:#3366ff;">create, read, update, delete</span>). Thật tuyệt, và bạn chỉ mất có 5 phút. Nhưng liệu tôi có thể thay đổi một số thứ không?</p>
<h5><strong>Tùy biến List Page Template</strong></h5>
<p>List template được đặt trong  folder DynamicData/PageTemplates. Trang List mặc nhiên trông đã khá tốt rồi, nhưng tôi vẫn muốn thử xem liệu có thể thay đổi một vài thứ hay không. Vậy nên tôi mởi file List.aspx và thực hiện một số thay đổi.</p>
<ul>
<li>Đầu tiên, tôi thêm một viền đen bằng cách đặt GridView vào trong một dãy các thẻ DIV dùng hình nền để tạo kiểu cho đường viền. </li>
<li>Tiếp theo, tôi tăng PageSize từ 10 lên 15.</li>
<li>Sau đó thêm một Data Pager.</li>
<li>Cuối cùng, tôi muốn trang web này là chỉ đọc, nên tôi xóa các liên kết Insert, Edit và Delete.</li>
</ul>
<p>Chỉ là những thay đổi nhỏ, nhưng giao diện đã được cải tiến.</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_27ed648b-0f0e-4ffd-a394-260cd9018e50.png" alt="image" width="734" height="512" /></p>
<h5><strong>Tùy biến Details Page Template</strong></h5>
<p>Tiếp theo, tôi thay đổi trang Details một chút theo cách tương tự.</p>
<p><img src="http://mattberseth.com/WindowsLiveWriter/62285cb8f376_10F68/image_abdff0aa-6d50-41c4-a956-3bc8652e30f0.png" alt="image" width="731" height="502" /></p>
<h5><strong>Thêm Custom Metadata vào Model</strong></h5>
<p>Có một vài thay đổi sẽ làm bạn thấy các trang của tôi khác với các trang mặc nhiên.</p>
<ul>
<li>Số cột khác nhau trong các bảng dữ liệu (các trang của tôi có ít cột hơn)</li>
<li>Một số tiêu đề cột tên khác</li>
<li>Một số giá trị trong các ô được định dạng khác</li>
<li>Tên hiển thị của các bảng</li>
</ul>
<p>Để tùy biến các mục trên, bạn cần tạo một lớp metadata mà nó sẽ cung cấp cho hệ thống Dynamic Data thông tin về các thực thể của bạn. Thực sự mà nói, tôi không rành phần này lắm, có lẽ vì nó mới. Nhưng dù sao để làm được việc này, bạn cần phải tạo thêm hai lớp nữa cho mỗi lớp trong DataContext. Vậy nên tôi sẽ phải tạo thêm 10 lớp nữa cho 5 bảng dữ liệu của tôi.</p>
<p>Đầu tiên bạn cần tạo thêm một lớp partial với cùng tên của lớp entity trong mô hình dữ liệu và sau đó áp dụng một thuộc tính lên lớp này để có thể chỉ ra lớp metadata cho lớp này. Đoạn code dưới đây sẽ thực hiện điều này cho thực thể Order trong ứng dụng mẫu. The display name for the Order table is 'My Orders' - Applied with the TableName attributes</p>
<ul>
<li>Các cột OrderDate, ShippedDate, ShipAddress và ShipCity được thay đổi tên hiển thị bằng cách áp dụng thuộc tính DisplayName.</li>
<li>Các cột OrderDate và ShippedDate có định dạng được tùy biến- áp dụng bằng thuộc tính DisplayFormat.</li>
<li>Các cột RequiredDate, ShipVia, Freight, ShipName, ShipPostalCode, và ShipCountry được ẩn bởi khỏi giao diện bằng cách dùng thuộc tính ScaffoldColumn.</li>
</ul>
<p><span style="color:#606060;">1:</span> <span style="color:#008000;">//  Attach the OrderMetadata to the Order class</span></p>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> [MetadataType(<span style="color:#0000ff;">typeof</span>(OrderMetadata))]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> Order {}</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> [TableName(<span style="color:#006080;">"My Orders"</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> OrderMetadata</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#008000;">//  Override the display name</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     [DisplayName(<span style="color:#006080;">"Date Ordered"</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     <span style="color:#008000;">//  Format the Date</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     [DisplayFormat(DataFormatString=<span style="color:#006080;">"{0:d}"</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> OrderDate { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     <span style="color:#008000;">//  Override the display name</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     [DisplayName(<span style="color:#006080;">"Date Shipped"</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     <span style="color:#008000;">//  Format the Date</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     [DisplayFormat(DataFormatString = <span style="color:#006080;">"{0:d}"</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ShippedDate { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     <span style="color:#008000;">//  Override the display name</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>     [DisplayName(<span style="color:#006080;">"Address"</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ShipAddress { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>     <span style="color:#008000;">//  Override the display name</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>     [DisplayName(<span style="color:#006080;">"City"</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ShipCity { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span> </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>     <span style="color:#008000;">//  Columns I want hidden</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>     [ScaffoldColumn(<span style="color:#0000ff;">false</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> RequiredDate { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>     [ScaffoldColumn(<span style="color:#0000ff;">false</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ShipVia { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span>     [ScaffoldColumn(<span style="color:#0000ff;">false</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  31:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> Freight { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  32:</span>     [ScaffoldColumn(<span style="color:#0000ff;">false</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  33:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ShipName { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  34:</span>     [ScaffoldColumn(<span style="color:#0000ff;">false</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  35:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ShipPostalCode { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  36:</span>     [ScaffoldColumn(<span style="color:#0000ff;">false</span>)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  37:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> ShipCountry { get; set; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  38:</span> }</pre>
<h4>Kết luận</h4>
<p> Ok, đây là lần đầu thử qua Dynamic Data. Tôi thực sự hài lòng và tôi sẽ tiếp tục tìm hiểu thêm về nó.</p>
<p>Chúc bạn thành công.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Sử dụng LINQ to SQL]]></title>
<link>http://namdh.wordpress.com/?p=359</link>
<pubDate>Wed, 08 Oct 2008 08:31:06 +0000</pubDate>
<dc:creator>namdh</dc:creator>
<guid>http://namdh.it.wordpress.com/2008/10/08/s%e1%bb%ad-d%e1%bb%a5ng-linq-to-sql/</guid>
<description><![CDATA[Đây là bài viết đầu tiên trong loạt bài có chủ đề &#8220;LINQ to SQL&#8221;, các]]></description>
<content:encoded><![CDATA[<p><span style="color:#000000;">Đây là bài viết đầu tiên trong loạt bài có chủ đề "LINQ to SQL", các bài này sẽ cho bạn một cái nhìn khái quát, giúp bạn làm quen với LINQ, một trong những công nghệ mới có trong .NET 3.5.</span></p>
<p><span style="color:#000000;">Loạt bài này được dựa trên loạt Tutorial của ScottGu (</span><a href="http://weblogs.asp.net/scottgu"><span style="color:#0000ff;">http://weblogs.asp.net/scottgu</span></a><span style="color:#000000;">).</span></p>
<p><!--more--></p>
<h3><span style="text-decoration:underline;">LINQ to SQL là gì?</span></h3>
<p>LINQ to SQL là một phiên bản hiện thực hóa của O/RM (object relational mapping) có bên trong .NET Framework bản "Orcas" (nay là .NET 3.5), nó cho phép bạn mô hình hóa một cơ sở dữ liệu dùng các lớp .NET. Sau đó bạn có thể truy vấn cơ sở dữ liệu (CSDL) dùng LINQ, cũng như cập nhật/thêm/xóa dữ liệu từ đó.</p>
<p>LINQ to SQL hỗ trợ đầy đủ transaction, view và các stored procedure (SP). Nó cũng cung cấp một cách dễ dàng để thêm khả năng kiểm tra tính hợp lệ của dữ liệu và các quy tắc vào trong mô hình dữ liệu của bạn.</p>
<h3><span style="text-decoration:underline;">Mô hình hóa CSDL dùng LINQ to SQL:</span></h3>
<p>Visual Studio "Orcas" đã tích hợp thêm một trình thiết kế LINQ to SQL như một công cụ dễ dàng cho việc mô hình hóa một cách trực quan các CSDL dùng LINQ to SQL.  Bài viết sau sẽ đi sâu hơn vào cách dùng trình thiết kế này (bạn cũng có thể <a href="http://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspx" target="_blank">xem đoạn video này</a> để xem cách tôi tạo một mô hình LINQ to SQL). </p>
<p>Bằng cách dùng trình thiết kế LINQ to SQL, tôi có thể dễ dàng tạo một mô hình cho CSDL mẫu "Northwind" giống như dưới đây:</p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step1.jpg" alt="" /></p>
<p>Mô hình LINQ to SQL ở trên định nghĩa bốn lớp thực thể: Product, Category, Order và OrderDetail. Các thuộc tính của mỗi lớp ánh xạ vào các cột của bảng tương ứng trong CSDL. Mỗi instance của một lớp biểu diễn một dòng trong bảng dữ liệu.</p>
<p>Các mũi tên giữa bốn lớp thực thể trên biểu diễn quan hệ giữa các thực thể khác nhau, chúng được tạo ra dựa trên các mối quan hệ primary-key/foreign-key trong CSDL. Hướng của mũi tên chỉ ra mối quan hệ là một - một hay một - nhiều. Các thuộc tính tương ứng sẽ được thêm vào các lớp thực thể trong các trường hợp này. Lấy ví dụ, lớp Category ở trên có một mối quan hệ một nhiều với lớp Product, điều này có nghĩa nó sẽ có một thuộc tính "Categories" là một tập hợp các đối tượng Product trong Category này. Lớp Product cũng sẽ có một thuộc tính "Category" chỉ đến đối tượng "Category" chứa Product này bên trong.</p>
<p>Bảng các phương thức bên tay phải  bên trong trình thiết kế LINQ to SQL ở trên chứa một danh sách các SP để tương tác với mô hình dữ liệu của chúng ta. Trong ví dụ trên tôi đã thêm một thủ tục có tên "GetProductsByCategory". Nó nhận vào một categoryID và trả về một chuỗi các Product. Chúng ta sẽ xem bằng cách nào có thể gọi được thủ tục này trong một đoạn code bên dưới.</p>
<p><span style="text-decoration:underline;">Tìm hiểu lớp DataContext</span></p>
<p>Khi bạn bấm nút "Save" bên trong màn hình thiết kế LINQ to SQL, Visual Studio sẽ lưu các lớp .NET biểu diễn các thực thể và quan  hệ bên trong CSDL mà chúng ta vừa mô hình hóa. Cứ mỗi một file LINQ to SQL chúng ta thêm vào solution, một lớp DataContext sẽ được tạo ra, nó sẽ được dùng khi cần truy vấn hay cập nhật lại các thay đổi. Lớp DataContext được tạo sẽ có các thuộc tính để biểu diễn mối bảng được mô hình hóa từ CSDL, cũng như các phương thức cho mỗi SP mà chúng ta đã thêm vào.</p>
<p>Lấy ví dụ, dưới đây là lớp NorthwindDataContext được sinh ra dựa trên mô hình chúng ta tạo ra ở trên:</p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step2.jpg" alt="" /></p>
<h3><span style="text-decoration:underline;">Các ví dụ LINQ to SQL</span></h3>
<p>Một khi đã mô hình hóa CSDL dùng trình thiết kế LINQ to SQL, chúng ta có thể dễ dàng viết các đoạn lệnh để làm việc với nó. Dưới đây là một vài ví dụ về các thao tác chung khi xử lý dữ liệu:</p>
<h4><span style="text-decoration:underline;">1) Lấy các Product từ CSDL</span></h4>
<p>Đoạn lệnh dưới đây dùng cú pháp LINQ để lấy về một tập IEnumerable các đối tượng Product. Các sản phẩm được lấy ra phải thuộc phân loại "Beverages":</p>
<p><em>C#:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step3.jpg" alt="" /></p>
<p><em>VB:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step9.jpg" alt="" /></p>
<h4><span style="text-decoration:underline;">2) Cập nhật một sản phẩm trong CSDL</span></h4>
<p>Đoạn lệnh dưới đây cho thấy cách lấy một sản phẩm, cập nhật lại giá tiền và lưu lại CSDL.</p>
<p><em>C#:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step5.jpg" alt="" /></p>
<p><em>VB:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step11.jpg" alt="" /></p>
<h4><span style="text-decoration:underline;">3) Chèn thêm một phân loại mới và hai sản phẩm vào CSDL</span></h4>
<p>Đoạn mã dưới đây biểu diễn cách tạo một phân loại mới, và tạo hai sản phẩm mới và đưa chúng vào trong phân loại đã tạo. Cả ba sau đó sẽ được đưa vào cơ sở dữ liệu.</p>
<p>Chú ý rằng tôi không cần phải tự quản lý các mối quan hệ primary key/foreign key, thay vào đó, tôi chỉ đơn giản thêm các đối tượng Product vào tập hợp Products của đối tượng category, và rồi thêm đối tượng category vào tập hợp Categories của DataContext, LINQ to SQL sẽ biết cách thiết lập các giá trị primary key/foreign key một cách thích hợp.</p>
<p><em>C# </em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step4.jpg" alt="" /></p>
<p><em>VB:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step12.jpg" alt="" /></p>
<h4><span style="text-decoration:underline;">4) Xóa các sản phẩm</span></h4>
<p>Đoạn mã sau sẽ biểu diễn cách xóa tất cả các sản phẩm Toy khỏi CSDL:</p>
<p><em>C#:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step6.jpg" alt="" /></p>
<p><em>VB:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step13.jpg" alt="" /></p>
<h4><span style="text-decoration:underline;">5) Gọi một thủ tục</span></h4>
<p>Đoạn mã dưới đây biểu diễn cách lấy các thực thể Product mà không dùng cú pháp của LINQ, mà gọi đến thủ tục "GetProductsByCategory" chúng ta đã thêm vào trước đây. Nhớ rằng một khi đã lấy về kết quả, tôi có thể cập nhật/xóa và sau đó gọi db.SubmitChanges() để cập nhật các thay đổi trở lại CSDL.</p>
<p><em>C#:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step7.jpg" alt="" /></p>
<p><em>VB:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step14.jpg" alt="" /></p>
<h4><span style="text-decoration:underline;">6) Lấy các sản phẩm và phân trang</span></h4>
<p>Đoạn mã dưới đây biểu diễn cách phân trang trên server như một phần của câu truy vấn LINQ. Bằng cách dùng các toán tử Skip() và Take(), chúng ta sẽ chỉ trả về 10 dòng từ CSDL - bắt đầu từ dòng 200.</p>
<p><em>C#:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step8.jpg" alt="" /></p>
<p><em>VB:</em></p>
<p><img src="http://www.scottgu.com/blogposts/linqtosql/step10.jpg" alt="" /></p>
<h3><span style="text-decoration:underline;">Tổng kết</span></h3>
<p>LINQ to SQL cung cấp một cách hay, rõ ràng để mô hình hóa lớp dữ liệu trong ứng dụng của bạn. Một khi đã định nghĩa  mô hinh dữ liệu, bạn có thể dễ dàng thực hiện các câu truy vấn cũng như cập nhật, xóa, sửa dữ liệu một cách hiệu quả.</p>
<p>Hi vọng những hướng dẫn và ví dụ mẫu ở trên đã giúp bạn làm quen với LINQ. Tôi sẽ tiếp tục các bài viết này để giúp bạn khám phá LINQ to SQL một cách chi tiết hơn.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[How to excute a LINQ Expression]]></title>
<link>http://iron9light.wordpress.com/?p=56</link>
<pubDate>Wed, 08 Oct 2008 06:15:16 +0000</pubDate>
<dc:creator>iron9light</dc:creator>
<guid>http://iron9light.it.wordpress.com/2008/10/08/how-to-excute-a-linq-expression/</guid>
<description><![CDATA[Linq for .NET 3.5 is a very interesting new feature. It provide you some abilities that compiler has]]></description>
<content:encoded><![CDATA[<p>Linq for .NET 3.5 is a very interesting new feature. It provide you some abilities that compiler has.</p>
<p>Thanks for Expression (under System.Linq.Expressions namespace), we can keep the expression tree but not compiled MSIL code.</p>
<p>When you use Linq like this,</p>
<p>[code language='csharp']</p>
<p>var count = from dataItem in data where dataItem.Value &#62; 100 select data.Value2;</p>
<p>[/code]</p>
<p>You do not need to know the expression tree (You even do not need to add the using of System.Linq.Expressions), .NET compiler and runtime to all the things for you.</p>
<p>That good most of the times.</p>
<p>Sometimes, you need to do something by yourself. Just like my current project, I need get the result directly from a expression tree.</p>
<p>[code language='csharp']</p>
<p>object GetResult(Expression m);</p>
<p>T GetResult<T>(Expression m);</p>
<p>[/code]</p>
<p>The only one I can find to do this directly is Excute method in IQueryProvider interface.</p>
<p>[code language='csharp']</p>
<p>public interface IQueryProvider<br />
{<br />
Object Excute(Expression expression);</p>
<p>TResult Excute<br />
(Expression expression);<br />
}</p>
<p>[/code]</p>
<p>Looks great, but useless. It use an interface, and I cannot find a impliment in the framework.</p>
<p>Ok, it's time to show you the truth. No more words. Just the code below.</p>
<p>[code language='csharp']</p>
<p>public static object GetResult(this Expression m)<br />
{<br />
    if (m == null)<br />
        throw new ArgumentException("m");<br />
    Type type = typeof (Func<>).MakeGenericType(m.Type);<br />
    LambdaExpression lambda = Expression.Lambda(type, m);<br />
    Delegate callback = lambda.Compile();<br />
    object result = callback.DynamicInvoke();<br />
    return result;<br />
}</p>
<p>public static T GetResult<T>(this Expression m)<br />
{<br />
    if (m == null)<br />
        throw new ArgumentNullException("m");<br />
    Expression<Func<T>> lambda = Expression.Lambda<Func<T>>(m);<br />
    Func<T> callback = lambda.Compile();<br />
    T result = callback();<br />
    return result;<br />
}</p>
<p>[/code]</p>
<p>Compile to MSIL at runtime, isn't it cool?!</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Randomly Sorting a List using Extension Methods]]></title>
<link>http://ducas.wordpress.com/2008/10/08/randomly-sorting-a-list-using-extension-methods/</link>
<pubDate>Tue, 07 Oct 2008 23:21:47 +0000</pubDate>
<dc:creator>ducas</dc:creator>
<guid>http://ducas.it.wordpress.com/2008/10/08/randomly-sorting-a-list-using-extension-methods/</guid>
<description><![CDATA[I was trawling through some old code I had written while doing some “refactoring” and came acros]]></description>
<content:encoded><![CDATA[<p>I was trawling through some old code I had written while doing some “refactoring” and came across this little nugget. I wanted to sort a list of objects that I was retrieving from a database using LINQ to SQL into a random order. Seeing as extension methods are all the rage, I decided to use them…</p>
<pre class="code"><span style="color:#0000ff;">public static class</span><span> ListExtensions</span> {
  <span style="color:#0000ff;">public static </span><span style="color:#2b91af;">IEnumerable</span>&#60;T&#62; Randomise&#60;T&#62;(<span style="color:#0000ff;">this </span><span style="color:#2b91af;">IEnumerable</span>&#60;T&#62; list) {
    <span style="color:#2b91af;">Random </span>rand = <span style="color:#0000ff;">new </span><span style="color:#2b91af;">Random</span>();
    <span style="color:#0000ff;">var </span>result = list.OrderBy(l =&#62; rand.Next());
    <span style="color:#0000ff;">return </span>result;
  }
}
</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>How does it work…? It adds the Randomise() extension method to the end of any IEnumerable&#60;T&#62; (e.g. List&#60;T&#62;) and uses the OrderBy function to change the sort order based on a randomly generated number.</p>
<pre class="code">
<span style="color:#0000ff;">var </span>randomCategories = context.Categories.Randomise();
</pre>
<p>The above code will execute the Randomise function to reorder the list of Category objects retrieved from the context randomly and assign the result to randomCategories.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Linq Synchronizer]]></title>
<link>http://vgermain.wordpress.com/?p=80</link>
<pubDate>Tue, 07 Oct 2008 09:57:09 +0000</pubDate>
<dc:creator>Vincent Germain</dc:creator>
<guid>http://vgermain.it.wordpress.com/2008/10/07/linq-synchronizer/</guid>
<description><![CDATA[The first time I was using Linq To SQL, I was frustrated by the fact VS2008 did not integrated the ]]></description>
<content:encoded><![CDATA[<p>The first time I was using Linq To SQL, I was frustrated by the fact VS2008 did not integrated the more simple fonctionnality called : "Synchronize DBML Shema with Database". Just removing my classes from DBML Shema and perform a Drag&#38;Drop from Server Explorer to schema was to much for me ^^.</p>
<p>So I wrote a simple VS 2008 plug-in which allow with 2 clics a synchronization between my database and My DBML Schema.</p>
<p>Here the installer :</p>
<p><a href="http://vgermain.wordpress.com/files/2008/10/linqsynchronizer.ppt">Linq To SQL Synchronizer</a></p>
<p>Rename the .ppt file in .zip file.</p>
<p>After installing, the add-in file must be define in userProfile/documents/VS2008/Addins/ folder and the assembly, for example, in program files folder.</p>
<p>If problems comes, be sure to check the path of the assembly in the .addin file(userProfile). It must reference the assembly in the program files(in this case).</p>
<p>The add-in is too simple that I haven't done a User Guide. I let you eval :-)</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[What is a SchemaSet?]]></title>
<link>http://kentmerrell.wordpress.com/?p=51</link>
<pubDate>Tue, 07 Oct 2008 02:54:03 +0000</pubDate>
<dc:creator>kentmerrell</dc:creator>
<guid>http://kentmerrell.it.wordpress.com/2008/10/07/what-is-a-schemaset/</guid>
<description><![CDATA[What is a SchemaSet?  Why do we use that instead of simply a Schema when validating an xdoc?
]]></description>
<content:encoded><![CDATA[<p>What is a SchemaSet?  Why do we use that instead of simply a Schema when validating an xdoc?</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[XML Validation with Linq]]></title>
<link>http://kentmerrell.wordpress.com/?p=45</link>
<pubDate>Tue, 07 Oct 2008 02:45:12 +0000</pubDate>
<dc:creator>kentmerrell</dc:creator>
<guid>http://kentmerrell.it.wordpress.com/2008/10/07/xml-validation-with-linq/</guid>
<description><![CDATA[The general principle is this&#8230;

XDocument xdoc = new Xdocument(...)
SchemaSet schemaSet = new ]]></description>
<content:encoded><![CDATA[<p>The general principle is this...</p>
<blockquote>
<pre>XDocument xdoc = new Xdocument(...)
SchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "filename.xsd");</pre>
</blockquote>
<p>Once you have a xdocument object and a schemaset object you call the Validate method of the xdocument objecct:</p>
<blockquote>
<pre>xdoc.Validate(schemaSet, null);</pre>
</blockquote>
]]></content:encoded>
</item>
<item>
<title><![CDATA[ListBoxi filtreerimine ja Bindable LINQ]]></title>
<link>http://h6bevalge.wordpress.com/2008/10/04/listboxi-filtreerimine-ja-bindable-linq/</link>
<pubDate>Sat, 04 Oct 2008 12:32:40 +0000</pubDate>
<dc:creator>h6bevalge</dc:creator>
<guid>http://h6bevalge.it.wordpress.com/2008/10/04/listboxi-filtreerimine-ja-bindable-linq/</guid>
<description><![CDATA[Mr. Leeti sahvrist leidsin huvitava viite: Bindable LINQ. Mõtlesin, et testin ja jagan. Bindable LI]]></description>
<content:encoded><![CDATA[<p>Mr. Leeti <a href="http://omasahver.blogspot.com/2008/10/future-plans-development.html">sahvrist</a> leidsin huvitava viite: Bindable LINQ. Mõtlesin, et testin ja jagan. Bindable LINQ <a href="http://www.codeplex.com/bindablelinq">Codeplexi</a> lehel on kirjutatud, et tugi on olemas Silverlight 2 Beta 1 versiooni jaoks aga tegelikult töötab ka Beta 2-ga.</p>
<h2>Ülesanne</h2>
<p>Kujutagem ette olukorda, kus näiteks ListBoxi sisuks on mingi järjendi sisu:</p>
<pre class="code"><span style="color:blue;">&#60;</span><span style="color:#a31515;">ListBox </span><span style="color:red;">x</span><span style="color:blue;">:</span><span style="color:red;">Name</span><span style="color:blue;">=&#34;listBox&#34; /&#62;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color:#2b91af;">List</span>&#60;<span style="color:blue;">string</span>&#62; nimed = <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&#60;<span style="color:blue;">string</span>&#62;();
nimed.Add(<span style="color:#a31515;">&#34;Jaana&#34;</span>);
nimed.Add(<span style="color:#a31515;">&#34;Indrek&#34;</span>);
nimed.Add(<span style="color:#a31515;">&#34;Ilse&#34;</span>);
nimed.Add(<span style="color:#a31515;">&#34;Jaan&#34;</span>);

listBox.ItemsSource = nimed;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code">nimed.Add(<span style="color:#a31515;">&#34;Lotte&#34;</span>);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>
  <br />See annab tulemuseks:</p>
<p><a href="http://h6bevalge.files.wordpress.com/2008/10/image4.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://h6bevalge.files.wordpress.com/2008/10/image-thumb4.png" width="142" height="114" /></a> </p>
<p>Siin ei ole aga elementi Lotte, mis sai lisatud peale listBoxi sidumist nimede järjendiga.</p>
<p>Üks lahendus oleks igal korral, kui andmestikku muudame, listBox ära nullida:</p>
<pre class="code">nimed.Add(<span style="color:#a31515;">&#34;Lotte&#34;</span>);
listBox.ItemsSource = <span style="color:blue;">null</span>;
listBox.ItemsSource = nimed;</pre>
<p>
  <br />Suhteliselt ebamugav aga ajab asja ära - nüüd jõuab Lotte ka nimekirja.</p>
<p>On olemas aga palju lihtsam viis – kasutada tavalise Listi asemel <em>ObservableCollection</em>-it, mis annab oma muudatustest automaatselt teada kõigile, kellega ta seotud on. Hetke näites, uueneb automaatselt listBoxi sisu. Ainus vajalik muudatus eelnevates koodilõikudes (ListBoxi nullima enam ei pea): </p>
<pre class="code"><span style="color:#2b91af;">ObservableCollection</span>&#60;<span style="color:blue;">string</span>&#62; nimed = <span style="color:blue;">new </span><span style="color:#2b91af;">ObservableCollection</span>&#60;<span style="color:blue;">string</span>&#62;();</pre>
<h2>
  <br />Samm edasi</h2>
<p>
  <br />Aga olgu meil nüüd nii, et ListBoxis on ainult osad nimed, näiteks sellised, mis vastavad mingile kindlale LINQ lausele.</p>
<p>Lisame näitele TextBoxi, mille abil filtreerime nimekirjast teatud nimed välja:</p>
<pre class="code"><span style="color:blue;">&#60;</span><span style="color:#a31515;">TextBox </span><span style="color:red;">x</span><span style="color:blue;">:</span><span style="color:red;">Name</span><span style="color:blue;">=&#34;_filterTextBox&#34;/&#62;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p></p>
<p>ListBoxi andmeallikaks saab nüüd järgnev LINQ päring:</p>
<pre class="code">listBox.ItemsSource = <span style="color:blue;">from </span>nimi <span style="color:blue;">in </span>nimed
                      <span style="color:blue;">where </span>nimi.StartsWith(_filterTextBox.Text)
                      <span style="color:blue;">select </span>nimi;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>
  <br />Mõte on ju hea aga see ei tööta. Tekib kaks probleemi:</p>
<ol>
<li>Kuigi nimed on ikka veel ObservableCollection-is, siis elementi Lotte listboxis ei ole (Lotte on lisatud pärast listBox.ItemsSource = … rida) </li>
<li>Kui midagi tekstikasti trükkida, siis ei toimu filtreerimist. </li>
</ol>
<h2>
  <br />Üks lahendus:</h2>
<p>
  <br />Jätta esialgu ikkagi listBox.ItemsSource = nimed ning lisada tekstikastile sündmusekuular:</p>
<pre class="code"><span style="color:blue;">&#60;</span><span style="color:#a31515;">TextBox </span><span style="color:red;">x</span><span style="color:blue;">:</span><span style="color:red;">Name</span><span style="color:blue;">=&#34;_filterTextBox&#34; </span><span style="color:red;">TextChanged</span><span style="color:blue;">=&#34;_filterTextBox_TextChanged&#34;/&#62;</span></pre>
<p>
  <br />Ja iga kord, kui see sündmus tekib muuta listBoxi andmeallikat:</p>
<pre class="code"><span style="color:blue;">private void </span>_filterTextBox_TextChanged(<span style="color:blue;">object </span>sender, <span style="color:#2b91af;">TextChangedEventArgs </span>e){    listBox.ItemsSource = <span style="color:blue;">from </span>nimi <span style="color:blue;">in </span>nimed                          <span style="color:blue;">where </span>nimi.StartsWith(_filterTextBox.Text)
                          <span style="color:blue;">select </span>nimi;}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Töötab, aga tegelikult on üks lahendus veel …</p>
<h2>Bindable LINQ</h2>
<p><em><br />
    <br /><a href="http://www.codeplex.com/bindablelinq">Bindable LINQ</a> is a set of extensions to LINQ that add data binding and change propagation capabilities to standard LINQ queries.</em> </p>
<p>Ehk <a href="http://www.codeplex.com/bindablelinq">BIndable LINQ</a> teeb LINQ päringud muudatustetundlikuks, ta saab aru, kui LINQ lause sõltub mingitest kolmandatest muutuvatest parameetritest. Siinses näites siis faktid, et nimede järjend võib muutuda ja et tekstikastist tulev tekst muutub. Bindable LINQ võtab kõike seda arvesse ning lahendab meie ülesande elegantselt:</p>
<pre class="code">listBox.ItemsSource = <span style="color:blue;">from </span>nimi <span style="color:blue;">in </span>nimed<strong>.<font color="#ff0000">AsBindable</font>()</strong>
                      <span style="color:blue;">where </span>nimi.StartsWith(FilterTextBox.Text)
                      <span style="color:blue;">select </span>nimi;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>See AsBindable() tulebki BinableLinq-st, mille saab tõmmata <a href="http://www.codeplex.com/bindablelinq">siit</a>. Kasutamiseks tuleb see lisada projekti: Project-&#62;Add Reference-&#62;Browse sinna, kus on BindableLINQ.dll, samuti tuleb lisada using Bindable.Linq;</p>
<h2>Kogu kood:</h2>
<p>
  <br /><strong>Page.xaml</strong></p>
<pre class="code"><span style="color:blue;">&#60;</span><span style="color:#a31515;">UserControl </span><span style="color:red;">x</span><span style="color:blue;">:</span><span style="color:red;">Class</span><span style="color:blue;">=&#34;Bindable.Page&#34;
    </span><span style="color:red;">xmlns</span><span style="color:blue;">=&#34;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#34;
    </span><span style="color:red;">xmlns</span><span style="color:blue;">:</span><span style="color:red;">x</span><span style="color:blue;">=&#34;http://schemas.microsoft.com/winfx/2006/xaml&#34;
    </span><span style="color:red;">Width</span><span style="color:blue;">=&#34;400&#34; </span><span style="color:red;">Height</span><span style="color:blue;">=&#34;300&#34;&#62;
    &#60;</span><span style="color:#a31515;">Grid </span><span style="color:red;">x</span><span style="color:blue;">:</span><span style="color:red;">Name</span><span style="color:blue;">=&#34;LayoutRoot&#34; </span><span style="color:red;">Background</span><span style="color:blue;">=&#34;White&#34;&#62;
        &#60;</span><span style="color:#a31515;">Grid.RowDefinitions</span><span style="color:blue;">&#62;
            &#60;</span><span style="color:#a31515;">RowDefinition </span><span style="color:red;">Height</span><span style="color:blue;">=&#34;40&#34;/&#62;
            &#60;</span><span style="color:#a31515;">RowDefinition </span><span style="color:red;">Height</span><span style="color:blue;">=&#34;*&#34;/&#62;
        &#60;/</span><span style="color:#a31515;">Grid.RowDefinitions</span><span style="color:blue;">&#62;
            &#60;</span><span style="color:#a31515;">TextBox </span><span style="color:red;">x</span><span style="color:blue;">:</span><span style="color:red;">Name</span><span style="color:blue;">=&#34;_filterTextBox&#34; </span><span style="color:red;">Grid.Row</span><span style="color:blue;">=&#34;0&#34; /&#62;
            &#60;</span><span style="color:#a31515;">ListBox </span><span style="color:red;">x</span><span style="color:blue;">:</span><span style="color:red;">Name</span><span style="color:blue;">=&#34;listBox&#34; </span><span style="color:red;">Grid.Row</span><span style="color:blue;">=&#34;1&#34;/&#62;
    &#60;/</span><span style="color:#a31515;">Grid</span><span style="color:blue;">&#62;
&#60;/</span><span style="color:#a31515;">UserControl</span><span style="color:blue;">&#62;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>
  <br /><strong>Page.xaml.cs</strong></p>
<pre class="code"><span style="color:blue;">using </span>System.Collections.ObjectModel;
<span style="color:blue;">using </span>System.Windows.Controls;
<span style="color:blue;">using </span>Bindable.Linq;

<span style="color:blue;">namespace </span>Bindable
{
    <span style="color:blue;">public partial class </span><span style="color:#2b91af;">Page </span>: <span style="color:#2b91af;">UserControl
    </span>{
        <span style="color:blue;">private </span><span style="color:#2b91af;">ObservableCollection</span>&#60;<span style="color:blue;">string</span>&#62; nimed;

        <span style="color:blue;">public </span>Page()
        {
            InitializeComponent();
            Loaded += <span style="color:blue;">new </span>System.Windows.<span style="color:#2b91af;">RoutedEventHandler</span>(Page_Loaded);
        }

        <span style="color:blue;">void </span>Page_Loaded(<span style="color:blue;">object </span>sender, System.Windows.<span style="color:#2b91af;">RoutedEventArgs </span>e)
        {
            nimed = <span style="color:blue;">new </span><span style="color:#2b91af;">ObservableCollection</span>&#60;<span style="color:blue;">string</span>&#62;();
            nimed.Add(<span style="color:#a31515;">&#34;Jaana&#34;</span>);
            nimed.Add(<span style="color:#a31515;">&#34;Indrek&#34;</span>);
            nimed.Add(<span style="color:#a31515;">&#34;Ilse&#34;</span>);
            nimed.Add(<span style="color:#a31515;">&#34;Jaan&#34;</span>);

            listBox.ItemsSource = <span style="color:blue;">from </span>nimi <span style="color:blue;">in </span>nimed.AsBindable()
                                  <span style="color:blue;">where </span>nimi.StartsWith(FilterTextBox.Text)
                                  <span style="color:blue;">select </span>nimi;

            nimed.Add(<span style="color:#a31515;">&#34;Lotte&#34;</span>);
        }

        <strong><span style="color:blue;">public </span><span style="color:#2b91af;">TextBox </span>FilterTextBox
        {
            <span style="color:blue;">get </span>{ <span style="color:blue;">return </span>_filterTextBox; }
        }</strong>
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>
  <br />Tähele tuleks panna kõige viimast meetodit – nimelt peab _filterTextBoxi avalikuks tegema, muidu Bindable LINQ tema väärtust kätte ei saa. </p>
<h2>Tulemus: </p>
</h2>
<p><a href="http://h6bevalge.files.wordpress.com/2008/10/image5.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://h6bevalge.files.wordpress.com/2008/10/image-thumb5.png" width="260" height="224" /></a> </p>
<p><a href="http://silverlight.services.live.com/invoke/60908/Bindable/iframe.html">Testi (Silverlight 2 RC 0)</a></p>
<p>Lae alla (<a href="http://texmex5.pri.ee/Silverlight/Bindable.zip">zip</a>) </p>
<h2>Lõppsõna<br />
  <br /></h2>
<p><a href="http://www.codeplex.com/bindablelinq">Binable LINQ</a> on lahe ning veel lahedam oleks, kui ta saaks päris LINQ loomulikuks osaks :).</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[NWorkspace / Linq Workspace Followup]]></title>
<link>http://rogeralsing.wordpress.com/?p=116</link>
<pubDate>Fri, 03 Oct 2008 19:49:54 +0000</pubDate>
<dc:creator>Roger Alsing</dc:creator>
<guid>http://rogeralsing.com/2008/10/03/nworkspace-linq-workspace-followup/</guid>
<description><![CDATA[I&#8217;m still playing around with the outcome my Linq Workspace Experiment.
And I have to say that]]></description>
<content:encoded><![CDATA[<p>I'm still playing around with the outcome my <a href="http://rogeralsing.com/2008/07/03/ddd-nworkspace-experiment/">Linq Workspace Experiment</a>.</p>
<p>And I have to say that I'm fairly pleased with the result, it's only a handful of code (less than 100 LoC) but it makes testing so much easier.</p>
<p>I'm so pleased with it that we will be using this in a fairly large live project, a large portion of every Swedish citizens that turn 18 will pass through this system :-)</p>
<p>It has enabled us to test all of our service methods with in mem data w/o ever changing,faking or mocking our repositories.</p>
<p>So cheers to both Erik Meijer for making Linq such a cool tool and to Jimmy Nilsson for inventing the workspace pattern :-)</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[108 hours down, 100 to go]]></title>
<link>http://jsarge.wordpress.com/?p=14</link>
<pubDate>Fri, 03 Oct 2008 01:58:34 +0000</pubDate>
<dc:creator>thesarge</dc:creator>
<guid>http://jsarge.it.wordpress.com/2008/10/03/108-hours-down-100-to-go/</guid>
<description><![CDATA[So we&#8217;ve chewed through 108 hours of the allotted time and well I don&#8217;t think we have a ]]></description>
<content:encoded><![CDATA[<p>So we've chewed through 108 hours of the allotted time and well I don't think we have a lot to show right now but a half a dozen concept projects that individually prove each piece of our project will work, it's the threading them all together and applying the "business" logic which is proving to be time consuming.</p>
<p>Michael organised to come over to my place after work on Thursday night so we can bounce a bunch of ideas of each other and then spend the night, I then organised for the both of us to take Friday off work so that we could focus on this a little more without those pesky work distractions :)</p>
<p>I think by the end of the day (well COB) we should have the better part of the application complete, or at least I hope that's the case!</p>
<p>I don't want to mention too much of what I've done as I think it will kind f give our app away, even though Michael has listed the three major technologies I'm using on his blog (damn you Mike) so no real technology updates.</p>
<p>What I learnt yesterday was that LINQ is supported on mobile devices, I'll never cease to be amazed at the .Net framework, good job CF team!  It's funny coming in to this I thought we were going to have to write a lot of stuff ourselves - I figured the Compact Framework was just going to be a really crippled / cut  down version of the regular framework and don't get me wrong - there are some things missing (GDI+ for one!) but 90% of the functionality I've needed has been there and I thought it was going to be a lot closer to say 50%, I'm pleasantly surprised.</p>
<p>Anyway got to run, got to get some code done whilst Michael is still asleep :)</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Những điểm nổi bật của Visual Studio 2008]]></title>
<link>http://fly2universe.wordpress.com/?p=161</link>
<pubDate>Thu, 02 Oct 2008 09:48:30 +0000</pubDate>
<dc:creator>fly2universe</dc:creator>
<guid>http://fly2universe.it.wordpress.com/2008/10/02/nh%e1%bb%afng-di%e1%bb%83m-n%e1%bb%95i-b%e1%ba%adt-c%e1%bb%a7a-visual-studio-2008/</guid>
<description><![CDATA[1. Miễn phí và có thể được sử dụng cho mục đích thương mại
Có rất nhiều ]]></description>
<content:encoded><![CDATA[<p><strong><span style="color:#0000ff;">1. Miễn phí và có thể được sử dụng cho mục đích thương mại</p>
<p></span></strong>Có rất nhiều thứ miễn phí dành cho bạn:<br />
- Công cụ miễn phí<br />
- Dịch vụ host miễn phí trên Popfly<br />
- Các bài hướng dẫn từ BDLC<br />
- Các control và ebook<br />
- Các đoạn code mẫu trong Coding4Fun Development Kit<br />
- Bộ công cụ P2P, và<br />
- Bộ công cụ C++ Game Development</p>
<p><strong><span style="color:#0000ff;">2. LINQ và các cải tiến ngôn ngữ</p>
<p></span></strong></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_14.png" border="0" alt="" /></div>
<p>Visual Studio Express 2008 hỗ trợ đầy đủ LINQ (Language INtergrated Query) cho phép việc thực hiện ngôn ngữ truy vấn (tương tự như SQL) trực tiếp trong ngôn ngữ Visual Basic và C#. Điều này có nghĩa là bạn có thể truy vấn một cơ sở dữ liệu dạng XML chẳng hạn như các RSS feeds hay các đối tượng bên trong bộ nhớ bằng cách sử dụng một cú pháp đơn giản. Đi cùng với LINQ là một số những cải tiến trong ngôn ngữ giúp đơn giản hoá công việc với các kiểu dữ liệu của .NET và điều này sẽ được thấy rõ khi ta làm việc với LINQ.</p>
<p><strong><span style="color:#0000ff;">3. Nhiều nội dung học miễn phí</p>
<p></span></strong></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_13.png" border="0" alt="" /></div>
<p>Beginner Developer Learning Center (BDLC) có các đoạn video cho 4 sản phẩm của Express Edition cũng như các bài tutorial về LINQ, WPF,...</p>
<p><strong><span style="color:#0000ff;">4. Bộ thiết kế web và công cụ Javascript mới</p>
<p></span></strong></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_11.png" border="0" alt="" /></div>
<p>Được xây dựng dựa trên các engine của Expression Web, Visual Web Developer Express designer bao gồm một design view mạnh với các tính năng mới như Split View để xem cả HTML code và designer, cửa sổ CSS Styles và Properties để thiết kế, xem trước, và apply các kiểu khác nhau.<br />
Công cụ Javascript có cải tiến hơn về Intellisense, suy luận kiểu dữ liệu, IntelliSense tham số, hỗ trợ các Ajax control, bộ debug được cải tiến với visualizers cho các dữ liệu của Javascript,...</p>
<p><strong><span style="color:#0000ff;">5. Windows Presentation Foundation</p>
<p></span></strong></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_10.png" border="0" alt="" /></p>
<div>Một điểm mới trong Visual Basic và Visual C# 2008 Express Edition là một bộ designer trực quan và các template project để xây dựng các ứng dụng WPF. Bạn có thể xây dựng các ứng dụng đẹp, có khả năng tương tác sử dụng các media, animation và đồ hoạ 2D/3D với WPF. Hơn nữa, bạn có thể sử dụng các control WPF trong các ứng dụng Windows Form. Do đó bạn có thể tạo ra các control nặng về mặt đồ hoạ, giao diện trong WPF và sau đó gắn chúng vào ứng dụng Windows Form của mình.</div>
</div>
<p><span style="color:#0000ff;"><strong>6. Bộ công cụ phát triển Game cho C++</p>
<p></strong></span></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_7.png" border="0" alt="" /></div>
<p><span style="color:#0000ff;"><strong>7. Hỗ trợ ASP.NET AJAX và các Ajax Control</p>
<p></strong></span></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_6.png" border="0" alt="" /></div>
<p>Trong Visual Web Developer Express 2008 có sẵn ASP.NET AJAX giúp bạn có thể dễ dàng thực hiện công việc cập nhật partial-page (cập nhật bằng cách nhận dữ liệu động từ server mà không cần phải load lại trang) chỉ với 1 thao tác là add control UpdatePanel vào. Hơn nữa, bạn có thể sử dụng lại tất cả bộ ASP.NET AJAX Control Toolkit để trang web có các tính năng RAD AJAX như animation, đổ bóng (drop shadows), góc bo tròn (rounded corner),...</p>
<p><span style="color:#0000ff;"><strong>8. Nâng cao hiệu suất hoạt động</p>
<p></strong></span>Trong phiên bản này nhóm phát triển đã cố gắng tăng hiệu suất hoạt động cho các công việc như Visual Basic background compiler hay C# IntelliSense. Bạn sẽ cảm nhận sự khác biệt khi phát triển các dự án lớn.</p>
<p><span style="color:#0000ff;"><strong>9. Killer Content</p>
<p></strong></span></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_12.png" border="0" alt="" /></div>
<p>Bộ Coding4Fun Developer Toolkit cung cấp nhiều sample và resource hỗ trợ Windows Vista như Telephony, Mail, Calendar,...</p>
<p><span style="color:#0000ff;"><strong>10. Những "đồ chơi" miễn phí</p>
<p></strong></span></p>
<div><img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Top10ThingstoloveaboutVisualStudio2008Ex_C66D/image_thumb_4.png" border="0" alt="" /></div>
<p>Khi bạn register bộ Visual Studio Express, bạn sẽ nhận được<br />
- Các control miễn phí như Telerik Rad Ribbon Bar để xây dựng các ứng dụng có giao diện Office 2007,...<br />
- Các hình ảnh, icon miễn phí<br />
- Ebook miễn phí<br />
- Giảm giá<br />
Sau khi đăng ký xong hãy đến trang <a href="http://connect.microsoft.com/" target="_blank"><span style="color:#22229c;">http://connect.microsoft.com</span></a> để download</p>
<p style="text-align:right;">(nguồn : <a href="http://ciren.vn/forums/showpost.php?p=2756&#38;postcount=5">http://ciren.vn/forums/showpost.php?p=2756&#38;postcount=5</a>)<!-- / message --><!-- sig --></p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Asynchronous Models]]></title>
<link>http://focuspark.wordpress.com/?p=19</link>
<pubDate>Thu, 02 Oct 2008 04:49:32 +0000</pubDate>
<dc:creator>focuspark</dc:creator>
<guid>http://focuspark.it.wordpress.com/2008/10/02/asynchronous-models/</guid>
<description><![CDATA[I&#8217;ve started delving into the world of Linq and functional programming and realized how easy f]]></description>
<content:encoded><![CDATA[<p>I've started delving into the world of Linq and functional programming and realized how easy functional programing makes asynchonous programming when you combine it with generics. I've been working on a program that downloads files from a remote server, compresses them, and indexes them into a database for later retrieval. Basically, we're trying to speed up remote offices' network access to files they need. (By the way, we're using Microsoft's cabinet compression and it's been pretty amazing. For large binary files it's been as much as 20% better than bzip2). Since I can't show the actual code here, I thought I'd mock up a simple example doing factorials asychronously.</p>
<p>I've even gone so far as to call the same method three different ways to show how easy it is to go from 'traditional' code design to C#'s new functional format using lambda opterators. For those of you who are fluent in JavasScript, this should feel fairly normal.</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">class</span> Program {</pre>
<pre><span class="lnum">   2:  </span>        <span class="kwrd">static</span> <span class="kwrd">void</span> Main( <span class="kwrd">string</span>[ ] args ) {</pre>
<pre class="alt"><span class="lnum">   3:  </span>            Func&#60;<span class="kwrd">int</span>, <span class="kwrd">long</span>&#62; factorial = ( <span class="kwrd">int</span> num ) =&#62; {</pre>
<pre><span class="lnum">   4:  </span>                <span class="kwrd">long</span> <span class="kwrd">value</span> = 1;</pre>
<pre class="alt"><span class="lnum">   5:  </span>                <span class="kwrd">for</span> ( <span class="kwrd">int</span> i = 1 ; i &#60;= num ; i++ ) {</pre>
<pre><span class="lnum">   6:  </span>                    <span class="kwrd">value</span> *= i;</pre>
<pre class="alt"><span class="lnum">   7:  </span>                }</pre>
<pre><span class="lnum">   8:  </span>                <span class="kwrd">return</span> <span class="kwrd">value</span>;</pre>
<pre class="alt"><span class="lnum">   9:  </span>            };</pre>
<pre><span class="lnum">  10:  </span> </pre>
<pre class="alt"><span class="lnum">  11:  </span>            <span class="kwrd">for</span> ( <span class="kwrd">int</span> i = 10 ; i &#62; 0 ; i-- ) {</pre>
<pre><span class="lnum">  12:  </span>                Console.WriteLine(<span class="str">"A: {0} factorial is {1}"</span>, i, factorial(i));</pre>
<pre class="alt"><span class="lnum">  13:  </span>                (<span class="kwrd">new</span> Runner&#60;<span class="kwrd">int</span>, <span class="kwrd">long</span>&#62;(<span class="str">'B'</span>)).Do(( <span class="kwrd">int</span> num ) =&#62; { <span class="kwrd">long</span> <span class="kwrd">value</span> = 1; <span class="kwrd">for</span> ( <span class="kwrd">int</span> f = 1 ; f &#60;= num ; f++ ) <span class="kwrd">value</span> *= f; <span class="kwrd">return</span> <span class="kwrd">value</span>; }, i, ( <span class="kwrd">char</span> id, <span class="kwrd">int</span> initial, <span class="kwrd">long</span> final ) =&#62; { Console.WriteLine(<span class="str">"{0}: {1} factorial is {2}"</span>, id, initial, final); });</pre>
<pre><span class="lnum">  14:  </span>                (<span class="kwrd">new</span> Runner&#60;<span class="kwrd">int</span>, <span class="kwrd">long</span>&#62;(<span class="str">'D'</span>)).Do(factorial, i, ( <span class="kwrd">char</span> id, <span class="kwrd">int</span> initial, <span class="kwrd">long</span> final ) =&#62; { Console.WriteLine(<span class="str">"{0}: {1} factorial is {2}"</span>, id, initial, final); });</pre>
<pre class="alt"><span class="lnum">  15:  </span>                (<span class="kwrd">new</span> Runner&#60;<span class="kwrd">int</span>, <span class="kwrd">long</span>&#62;(<span class="str">'E'</span>)).Do(factorial, i, CallbackTo);</pre>
<pre><span class="lnum">  16:  </span>            }</pre>
<pre class="alt"><span class="lnum">  17:  </span> </pre>
<pre><span class="lnum">  18:  </span>            Console.Read();</pre>
<pre class="alt"><span class="lnum">  19:  </span>        }</pre>
<pre><span class="lnum">  20:  </span> </pre>
<pre class="alt"><span class="lnum">  21:  </span>        <span class="kwrd">static</span> <span class="kwrd">void</span> CallbackTo( <span class="kwrd">char</span> id, <span class="kwrd">int</span> initial, <span class="kwrd">long</span> final ) {</pre>
<pre><span class="lnum">  22:  </span>            Console.WriteLine(<span class="str">"{0}: {1} factorial is {2}"</span>, id, initial, final);</pre>
<pre class="alt"><span class="lnum">  23:  </span>        }</pre>
<pre><span class="lnum">  24:  </span>    }</pre>
<pre class="alt"><span class="lnum">  25:  </span> </pre>
<pre><span class="lnum">  26:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> Runner&#60;T, R&#62; {</pre>
<pre class="alt"><span class="lnum">  27:  </span>        <span class="kwrd">private</span> Func&#60;T, R&#62; worker;</pre>
<pre><span class="lnum">  28:  </span>        <span class="kwrd">private</span> T initial;</pre>
<pre class="alt"><span class="lnum">  29:  </span>        <span class="kwrd">private</span> <span class="kwrd">char</span> id;</pre>
<pre><span class="lnum">  30:  </span> </pre>
<pre class="alt"><span class="lnum">  31:  </span>        <span class="kwrd">public</span> Runner( <span class="kwrd">char</span> id ) {</pre>
<pre><span class="lnum">  32:  </span>            <span class="kwrd">this</span>.id = id;</pre>
<pre class="alt"><span class="lnum">  33:  </span>        }</pre>
<pre><span class="lnum">  34:  </span> </pre>
<pre class="alt"><span class="lnum">  35:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Do( Func&#60;T, R&#62; worker, T <span class="kwrd">value</span>, Callback callback ) {</pre>
<pre><span class="lnum">  36:  </span>            <span class="kwrd">this</span>.worker = worker;</pre>
<pre class="alt"><span class="lnum">  37:  </span>            <span class="kwrd">this</span>.initial = <span class="kwrd">value</span>;</pre>
<pre><span class="lnum">  38:  </span>            Thread thread = <span class="kwrd">new</span> Thread(<span class="kwrd">new</span> ThreadStart(( ) =&#62; {</pre>
<pre class="alt"><span class="lnum">  39:  </span>                R final = worker(<span class="kwrd">value</span>);</pre>
<pre><span class="lnum">  40:  </span>                callback(id, initial, final);</pre>
<pre class="alt"><span class="lnum">  41:  </span>            }));</pre>
<pre><span class="lnum">  42:  </span>            thread.Start();</pre>
<pre class="alt"><span class="lnum">  43:  </span>        }</pre>
<pre><span class="lnum">  44:  </span> </pre>
<pre class="alt"><span class="lnum">  45:  </span>        <span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">void</span> Callback( <span class="kwrd">char</span> id, T intial, R final );</pre>
<pre><span class="lnum">  46:  </span>    }</pre>
</div>
<p>As you can see, I use generics to define a worker function and then use a generic class to do fire off a thread that'll call back to the main thread when it's complete. Fairly straight foreward. I even threw in a standard call to a "normal" synchoronous method call for comparison.</p>
<p>The output from the test looks like this:<br />
 </p>
<pre style="font-family:'Courier New',Courier,mono;font-size:10px;">A: 5 factorial is 120
A: 10 factorial is 3628800
B: 10 factorial is 3628800
C: 10 factorial is 3628800
D: 10 factorial is 3628800
A: 9 factorial is 362880
E: 10 factorial is 3628800
B: 9 factorial is 362880
C: 9 factorial is 362880
D: 9 factorial is 362880
A: 8 factorial is 40320
E: 9 factorial is 362880
B: 8 factorial is 40320
C: 8 factorial is 40320
D: 8 factorial is 40320
A: 7 factorial is 5040
B: 7 factorial is 5040
E: 8 factorial is 40320
C: 7 factorial is 5040
D: 7 factorial is 5040
A: 6 factorial is 720
E: 7 factorial is 5040
B: 6 factorial is 720
C: 6 factorial is 720
D: 6 factorial is 720
A: 5 factorial is 120
E: 6 factorial is 720
B: 5 factorial is 120
C: 5 factorial is 120
D: 5 factorial is 120
A: 4 factorial is 24
E: 5 factorial is 120
B: 4 factorial is 24
C: 4 factorial is 24
D: 4 factorial is 24
A: 3 factorial is 6
E: 4 factorial is 24
B: 3 factorial is 6
C: 3 factorial is 6
D: 3 factorial is 6
A: 2 factorial is 2
E: 3 factorial is 6
B: 2 factorial is 2
C: 2 factorial is 2
D: 2 factorial is 2
A: 1 factorial is 1
E: 2 factorial is 2
B: 1 factorial is 1
C: 1 factorial is 1
D: 1 factorial is 1
E: 1 factorial is 1</pre>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Create a guest book using XML]]></title>
<link>http://rezatech.wordpress.com/?p=57</link>
<pubDate>Mon, 29 Sep 2008 16:32:53 +0000</pubDate>
<dc:creator>rezatech</dc:creator>
<guid>http://rezatech.it.wordpress.com/2008/09/29/create-a-guest-book-using-xml/</guid>
<description><![CDATA[Let us create a guest book that will use XML instead of Database table. This will include doing two ]]></description>
<content:encoded><![CDATA[<p>Let us create a <strong>guest book</strong> that will use <strong>XML</strong> instead of <strong>Database table</strong>. This will include doing two things.</p>
<p> </p>
<ol>
<li>Write to XML</li>
<li>Read from XML.</li>
</ol>
<h2><span style="text-decoration:underline;"><strong>Write to XML</strong></span></h2>
<p>The logic behind writing to XML for the guest book is</p>
<p> </p>
<ol>
<li>We have to check whether the desired XML is exists or not.</li>
<li>If it exists, that means this is not the first time entry. We have to append entries with the previous entry.</li>
<li>If it does not exist, that means the first guest is writing to the guest book. We have to create the XML and write to it.</li>
</ol>
<p> </p>
<p>let's consider the xml as <strong>"FeedbackList.xml"</strong> and we are going to put it in our<strong> C</strong> drive. To start lets make a web form with 4 textboxes. Those are txtName, txtEmail, txtCountry and txtMsg. And to see how the xml looks like lets take another multiline textbox into which we will display the resultant xml. Obviously you are not going to put this into your  final guest book. At this moment your page should look like below.</p>
<p><a href="http://rezatech.files.wordpress.com/2008/09/guestbook1.jpg"><img class="aligncenter size-full wp-image-59" title="guestbook.aspx" src="http://rezatech.wordpress.com/files/2008/09/guestbook1.jpg" alt="" width="1024" height="768" /></a>Now at the button click event write the following code.</p>
<p><a href="http://rezatech.files.wordpress.com/2008/09/ifblock.jpg"><img class="aligncenter size-full wp-image-60" title="if block" src="http://rezatech.wordpress.com/files/2008/09/ifblock.jpg" alt="" width="812" height="514" /></a></p>
<p>In the above code, system first checks whether the "FeedbackList.xml" exists in the C drive or not. If exixts, system adds one by one name, email, country and messages from the corresponding text boxes.</p>
<p>Finally, system displays the resultant xml into the txtOut text box and saves the original xml. Now lets have a look into the else block.</p>
<p><a href="http://rezatech.wordpress.com/files/2008/09/elseblock.jpg"><img class="aligncenter size-full wp-image-61" title="elseblock" src="http://rezatech.wordpress.com/files/2008/09/elseblock.jpg" alt="" width="590" height="328" /></a>In the above code, system fails to find the desired xml and that means this is first time any guest is writing message. So we are not going to use <strong>FeedbackList. Add </strong>this time. We will simply create new XElement, put data from the textboxes and save.</p>
<p>That is all. Run it and try to enter something and you will see some excellent output. Don't forget to include <strong>using System.XML.LINQ</strong> at the top.</p>
<h2><strong><span style="text-decoration:underline;">Read from XML</span></strong></h2>
<p>Now its time to read from the <strong>FeedbackList.xml</strong> into a nice Grid using the <strong>gridview</strong> control.</p>
<p>Let's make another webform and name it GuestBookData.aspx. Add a gridview control in it. Go to the code behind page and write the following code.</p>
<p><a href="http://rezatech.files.wordpress.com/2008/09/readfrxml.jpg"><img class="aligncenter size-full wp-image-64" title="readfrxml" src="http://rezatech.wordpress.com/files/2008/09/readfrxml.jpg" alt="" width="584" height="342" /></a></p>
<p>in the above code, system first loads the FeedbackList.xml from C drive. Then using LINQ we are fetching the values of every element and finally bind it to gridview. Run the page and you will something like this;</p>
<p><a href="http://rezatech.files.wordpress.com/2008/09/finaloutput.jpg"><img class="aligncenter size-full wp-image-65" title="finaloutput" src="http://rezatech.wordpress.com/files/2008/09/finaloutput.jpg" alt="" width="788" height="456" /></a></p>
<p>That's all. Isn't it cool and easy.</p>
<p>Happy coding mate.</p>
<p>cheers</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Combining Ordered Lists in .NET]]></title>
<link>http://alexreg.wordpress.com/?p=33</link>
<pubDate>Mon, 29 Sep 2008 16:02:26 +0000</pubDate>
<dc:creator>Alex</dc:creator>
<guid>http://alexreg.it.wordpress.com/2008/09/29/combining-ordered-lists/</guid>
<description><![CDATA[I recently came across an issue with LINQ involving the combination of ordered (sorted) lists. The p]]></description>
<content:encoded><![CDATA[<p>I recently came across an issue with LINQ involving the combination of ordered (sorted) lists. The problem does not seem to have a simple solution within the core libraries of .NET 3.5, so I decided to write my own (short) function to accomplish the task. Extensions methods and the static <em>Enumerable</em> class usually contain all the possible methods you might need for dealing with lists (or more generally enumerable collections). Combining ordered lists, however, isn't quite so straightforward a process (to do efficiently) as it might first seem. Of course, you could simply do <em>Enumerable.Concat(listA, listB).OrderBy(keySelector)</em> but it should be apparent that this is very inefficient for large lists if you know your lists are already ordered. Moreover, the call will never return if either or both of the enumerable collections you pass are of infinite length. What you really want to do is select items from the lists by switching back and forth between them, picking whichever of the next items ought to come first (which is determined by the key selector and associated IComparable implementation).</p>
<p>You can find the code for the function <a href="http://pastebin.ca/1213690">here</a>. For example, within a static class called <em>Enumerable2</em>, you can call it as such:</p>
<pre><code>var combinedList = Enumerable2.CombineOrdered(listA, listB, keySelector);</code></pre>
<p>Note: The given implementation needs to take lists sorted in an ascending order and returns a combined list in the same order. To sort descending, change the &#60;= sign to a &#62;= sign in the code and insure that you pass lists sorted in a descending order.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Update Detached Entities]]></title>
<link>http://pstatho.wordpress.com/2008/09/29/update-detached-entities/</link>
<pubDate>Mon, 29 Sep 2008 15:00:15 +0000</pubDate>
<dc:creator>pstatho</dc:creator>
<guid>http://pstatho.it.wordpress.com/2008/09/29/update-detached-entities/</guid>
<description><![CDATA[Probably one of the most asked questions with LINQ to SQL, is how do you update detached entities. O]]></description>
<content:encoded><![CDATA[<p>Probably one of the most asked questions with LINQ to SQL, is how do you update detached entities. Often people will simply resort to doing a SELECT to get the entity into the datacontext and then update the data.</p>
<p>Steve Michelotti describes how to do it here:</p>
<p><a title="http://geekswithblogs.net/michelotti/archive/2007/12/17/117791.aspx" href="http://geekswithblogs.net/michelotti/archive/2007/12/17/117791.aspx">http://geekswithblogs.net/michelotti/archive/2007/12/17/117791.aspx</a></p>
<p>and if your entity has child objects check this post out:</p>
<p><a title="http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx" href="http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx">http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx</a></p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Flex, .NET 3.5 with LINQ to SQL part 2 (synchronising from Flex to LINQ)]]></title>
<link>http://justinjmoses.wordpress.com/?p=113</link>
<pubDate>Mon, 29 Sep 2008 01:01:02 +0000</pubDate>
<dc:creator>Justin J. Moses</dc:creator>
<guid>http://justinjmoses.it.wordpress.com/2008/09/29/flex-net-35-with-linq-to-sql-part-2-synchronising-from-flex-to-linq/</guid>
<description><![CDATA[Right.
So, getting your objects from your .NET 3.5 Linq to SQL setup to Flex is fairly easy. Getting]]></description>
<content:encoded><![CDATA[<p>Right.</p>
<p>So, getting your objects from your .NET 3.5 Linq to SQL setup to Flex is fairly easy. Getting the changed data back and synchronised to your DB is a different story.</p>
<p>The great thing about LINQ is that the data you retrieve and modify in .NET will automatically sync back to the DB by calling the SubmitChanges() method on your context (Linq to SQL) object.</p>
<p>The problem is of course when this data comes back in from Flex, the system doesn't know what exactly has changed.</p>
<p>Now, the first thing to realise is that <strong><span style="color:#ff0000;">your Value Objects in Flex should NOT have the foreign key IDs in them.</span> </strong>Why? Because say in the Company/Employee example, if you submit a Company object back to the system, with each Employee in the list having both a CompanyID AND a Company object reference, how will the LINQ<strong> </strong>system know which is the correct reference?<strong><br />
</strong></p>
<p>These foreign IDs will try to get serialised on the remoting level and be ignored. Good. We're dealing with object references now, and foreign key ids are useless. If I want the foreign key of some employee, I would say: EmployeeObj.Company.CompanyID</p>
<p>Now, in .NET, I have a "Save()" method that I want to essentially insert/update the object and all objects within in.</p>
<p>Using the example before, say I have a company object, and that has a list of Employees. Then if I have a Save(Company c) method, I want the company object to insert/update, and the list of employees to automatically synchronise.</p>
<p>You can try deleting the old object and then inserting the new</p>
<p>So,</p>
<pre><span style="color:#ff6600;">SaveCompany(Company newCompany)</span>
<span style="color:#ff6600;">{
</span>
<span style="color:#ff6600;">DataClassesDataContext db = new DataClassesDataContext();</span>
<span style="color:#ff6600;">if (newCompany.CompanyID &#62; 0)</span>
<span style="color:#ff6600;">{
</span>
<span style="color:#ff6600;">Company oldCompany = db.Companies.Single&#60;Company&#62;(c =&#62; c.CompanyID = newCompany.CompanyID);</span>
<span style="color:#ff6600;">db.Companies.DeleteOnSubmit(oldCompany);</span>
<span style="color:#ff6600;">}</span>
<span style="color:#ff6600;">db.Companies.InsertOnSubmit(newCompany);</span>
<span style="color:#ff6600;">db.SubmitChanges();</span>
<span style="color:#ff6600;">}</span></pre>
<p>This will nicely handle an insert/update of the company object, but it won't deal with your Employee objects that well. Especially when each Employee references anthor table that essentially depicts there type./</p>
<p>Say each Employee has a foreign key reference to EmployeeType. Employee type is a static naming table that has a set of 5 records: "Manager", "Sales Coordinator", "Receptionist", "Consultant" and "Helpdesk". Then my Employee object will have an EmployeeType property (loaded via LINQ). Now, when this goes through LINQ, I believe during the serialisation, the creation of a "new" object EmployeeType will mean that the code above will create a new EmployeeType record for the Employee. Ouch.</p>
<p>So even though you may have sent all the employees through LINQ to Flex via a Company object, when it comes back, even if unchanged, all these employees will have references to new EmployeeType objects.</p>
<p>The way to solve this is fairly easy:</p>
<pre><span style="color:#ff6600;">foreach (Employee e in newCompany.Employees)
{
   EmployeeType type = db.EmployeeType.Single&#60;EmployeeType&#62;(t =&#62; t.TypeID == e.EmployeeType.TypeID);
   e.EmployeeType = type;
}

oldCompany.Employees = newCompany.Employees;</span></pre>
<p>Unfortunately, it's not an easy process to learn how exactly to handle each scenario in synching LINQ and Flex, but one thing's for sure, it certainly makes code lighter, and a lot easier to update.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[LinqDataSource with Select method!]]></title>
<link>http://aspguy.wordpress.com/?p=116</link>
<pubDate>Sun, 28 Sep 2008 16:26:48 +0000</pubDate>
<dc:creator>Aref Karimi</dc:creator>
<guid>http://aspguy.it.wordpress.com/2008/09/28/linqdatasource-with-select-method/</guid>
<description><![CDATA[

As I mentioned in the previous post, with Insert, Update and Delete methods but without a Select m]]></description>
<content:encoded><![CDATA[<p style="text-align:center;"><a href="http://secure.hostgator.com/cgi-bin/affiliates/clickthru.cgi?id=lollypop55" target="_blank"><img class="aligncenter" src="http://www.hostgator.com/affiliates/banners/hgator-468x60d.gif" alt="" /></a></p>
<p style="text-align:justify;">
<p style="text-align:justify;">As I mentioned in the previous post, with Insert, Update and Delete methods but without a Select method, LinqDataSource control looks just like a chair with only three legs! Specially when it comes to situations that you have to retrieve records programatically.</p>
<p style="text-align:justify;">I probed alot to see if there is a work around for this problem but found nothing. Therefore I wrote my own LinqDataSource that supports this fearure. I called this control GoodLinqDataSoruce !  GoodLinqDataSource contains a method called SelectRecords which returns a non-generic <span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"><span style="color:#2b91af;"><span style="color:#2b91af;">IEnumerable. </span></span></span></span>Selecting event event is triggered before the record retrieval is perforemd and afterwards Selected event is fired.</p>
<h3>How this control is implemented?</h3>
<p style="text-align:justify;">LinqDataSource contains a protected method called GetView(string viewname) which returns a <span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"><span style="color:#2b91af;"><span style="color:#2b91af;">DataSourceView</span></span> object. Infact, the retrieval actions is taken place with DataSourceView class. Moreover, LinqDataSource uses an extended version of </span></span><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"><span style="color:#2b91af;"><span style="color:#2b91af;">DataSourceView</span></span> named </span></span><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"><span style="color:#2b91af;"><span style="color:#2b91af;">LinqDataSourceView</span></span> . </span></span><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"><span style="color:#2b91af;"><span style="color:#2b91af;">LinqDataSourceView</span></span> has a Select method that performs record retrival and returns the result as an </span></span><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"><span style="color:#2b91af;"><span style="color:#2b91af;">IEnumerable</span></span></span></span>.</p>
<p>Since the source code is straighforward, I will bring it here :</p>
<p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed;"><span style="font-size:x-small;font-family:Courier New;color:blue;"><span style="font-size:10pt;font-family:&#34;">public</span></span><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"> <span style="color:blue;"><span style="color:blue;">class</span></span> <span style="color:#2b91af;"><span style="color:#2b91af;">GoodLinqDataSource</span></span> : <span style="color:#2b91af;"><span style="color:#2b91af;">LinqDataSource</span></span></span></span></p>
<p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed;"><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;">{</span></span></p>
<p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed;"><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"> <span style="color:blue;"><span style="color:blue;">private</span></span> <span style="color:#2b91af;"><span style="color:#2b91af;">LinqDataSourceView</span></span> DefaultView</span></span></p>
<p class="MsoNormal" style="text-align:left;direction:ltr;unicode-bidi:embed;"><span style="font-size:x-small;font-family:Courier New;"><span style="font-size:10pt;font-family:&#34;"> {</span></span></p>
<p cl