<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>LINQ</title>
        <link>http://geekswithblogs.net/Saqib/category/7520.aspx</link>
        <description>LINQ</description>
        <language>en</language>
        <copyright>Tanzim Saqib</copyright>
        <managingEditor>me@tanzimsaqib.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>LINQ to Flickr</title>
            <link>http://geekswithblogs.net/Saqib/archive/2008/03/01/linq-to-flickr.aspx</link>
            <description>&lt;p&gt;One of my colleagues &lt;a href="http://weblogs.asp.net/mehfuzh"&gt;Mehfuz Hossain&lt;/a&gt; developed a wonderful open source project which allows you to query Flickr photos by LINQ, also lets you insert, delete photos directly to/from Flickr. You wonder how to extend LINQ in such an amazing way? It’s easy by writing your own custom LINQ provider, which was not-so-easy until he came up with another handy open source project named &lt;a href="http://www.codeplex.com/linqextender"&gt;LINQ Extender&lt;/a&gt;&lt;a href="http://www.codeplex.com/linqextender"&gt;&lt;/a&gt;. He did all the expression parsing stuff to ease our pain. Now you can make your own LINQ to Anything using this so easily.&lt;/p&gt; &lt;p&gt;For your heads up on LINQ extenders, here &lt;a href="http://dotnetslackers.com/articles/csharp/CreatingCustomLINQProviderUsingLinqExtender.aspx"&gt;he wrote an article&lt;/a&gt; and &lt;a href="http://www.codeplex.com/LINQFlickr"&gt;LINQ to Flickr&lt;/a&gt;, open source project is hosted at Codeplex.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120119"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120119" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Saqib/aggbug/120119.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Tanzim Saqib</dc:creator>
            <guid>http://geekswithblogs.net/Saqib/archive/2008/03/01/linq-to-flickr.aspx</guid>
            <pubDate>Sat, 01 Mar 2008 16:08:11 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Saqib/comments/120119.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Saqib/archive/2008/03/01/linq-to-flickr.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Saqib/comments/commentRss/120119.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Saqib/services/trackbacks/120119.aspx</trackback:ping>
        </item>
        <item>
            <title>A &amp;quot;transactional&amp;quot; generic DbHelper for LINQ to SQL</title>
            <link>http://geekswithblogs.net/Saqib/archive/2008/02/29/a-quottransactionalquot-generic-dbhelper-for-linq-to-sql.aspx</link>
            <description>&lt;p&gt;In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language. You may want to make a data access layer that separates the data operation from business layer like the following:&lt;/p&gt;&lt;pre&gt;DbHelper.Insert&amp;lt;Student&amp;gt;(
    new Student()
    {
        FirstName = “Tanzim”,
        LastName = “Saqib”,
        Email = “me@TanzimSaqib.com”,
        Website = “http://www.TanzimSaqib.com”&lt;/pre&gt;&lt;pre&gt;}, true);    // Use Transaction?&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;&lt;p&gt;To make use of such transactional generic DbHelper, you might want to write a singeton DbHelper class like the following. You might notice that the class is singleton, it’s static and the DataContext is being used is private, and only initialized using the connection string if not yet.&lt;/p&gt;&lt;pre&gt;public static class DbHelper
{
    private const string CONNECTION_CONFIG_NAME = “StudentServerConnectionString”;

    private static StudentServerDataContext _StudentServerDataContext = null;

    public static StudentServerDataContext GetDataContext()
    {
        if(_StudentServerDataContext == null)
            _StudentServerDataContext = new StudentServerDataContext
                (ConfigurationManager.ConnectionStrings
                [CONNECTION_CONFIG_NAME].ConnectionString);

        return _StudentServerDataContext;
    }

    public static void CleanUp()
    {
        _StudentServerDataContext.Dispose();
        _StudentServerDataContext = null;
    }

    // … code edited to save space&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;&lt;p&gt;On application wide error or on end you can dispose the context so CleanUp method is useful here. To implement an Insert method see the following. You will find I have used a TransactionScope which ensures that the transaction is taking place without any interruption. If there is really any error the scope.Complete() method never gets invoked. This is how it ensures that the code inside the TransactionScope is taking place as a transaction. It is available from .NET 2.0 framework.&lt;/p&gt;&lt;pre&gt;public static void Insert&amp;lt;T&amp;gt;(T t, bool isTransactional) where T : class
{
    if (isTransactional)
    {
        using (var scope = new TransactionScope())
        {
            Insert&amp;lt;T&amp;gt;(t);

            // On any Exception, Complete() method won’t be invoked.
            // So, the transaction will be automatically rollbacked.
            scope.Complete();
        }
    }
    else
        Insert&amp;lt;T&amp;gt;(t);
}

public static void Insert&amp;lt;T&amp;gt;(T t) where T : class
{
    using (var db = GetDataContext())
    {
        db.GetTable&amp;lt;T&amp;gt;().InsertOnSubmit(t);

        try
        {
            db.SubmitChanges();
        }
        catch (Exception e)
        {
            // TODO: log Exception
            throw e;
        }
    }
}&lt;/pre&gt;
&lt;p&gt;I did not show other methods as part of the CRUD implementation. The rest is left open for you to implement.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120075"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120075" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Saqib/aggbug/120075.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Tanzim Saqib</dc:creator>
            <guid>http://geekswithblogs.net/Saqib/archive/2008/02/29/a-quottransactionalquot-generic-dbhelper-for-linq-to-sql.aspx</guid>
            <pubDate>Fri, 29 Feb 2008 02:29:09 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Saqib/comments/120075.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Saqib/archive/2008/02/29/a-quottransactionalquot-generic-dbhelper-for-linq-to-sql.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Saqib/comments/commentRss/120075.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Saqib/services/trackbacks/120075.aspx</trackback:ping>
        </item>
        <item>
            <title>LINQ: Iterate through collection which implements IEnumerable only</title>
            <link>http://geekswithblogs.net/Saqib/archive/2007/10/12/linq-iterate-through-collection-which-implements-ienumerable-only.aspx</link>
            <description>&lt;p&gt;The following works fine in LINQ, because an array implements IEnumerable &amp;lt;T&amp;gt;. &lt;br /&gt;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[] tokenArray = &lt;span style="COLOR: blue"&gt;new string&lt;/span&gt;[2] { &lt;span style="COLOR: #a31515"&gt;"Hello"&lt;/span&gt;, &lt;span style="COLOR: #a31515"&gt;"World" &lt;/span&gt;}; 
var tokens = from token &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;tokenList select token;              

&lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(var item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;tokens) 
    Console.WriteLine(item);&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
But, the collections which do not implement IEnumerable&amp;lt;T&amp;gt; or IQueryable, can not be iterated in LINQ in the same way. To achieve the same, make use of a simple casting trick such as: &lt;br /&gt;
&lt;/p&gt;
&lt;pre class="code"&gt;ArrayList tokenList = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;ArrayList(); 
tokenList.Add(&lt;span style="COLOR: #a31515"&gt;"Hello"&lt;/span&gt;); 
tokenList.Add(&lt;span style="COLOR: #a31515"&gt;"World"&lt;/span&gt;);              

var tokens = from &lt;span style="COLOR: blue"&gt;&lt;strong&gt;string&lt;/strong&gt; &lt;/span&gt;token &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;tokenList select token;              

&lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(var item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;tokens) 
    Console.WriteLine(item);&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=118412"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=118412" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Saqib/aggbug/118412.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Tanzim Saqib</dc:creator>
            <guid>http://geekswithblogs.net/Saqib/archive/2007/10/12/linq-iterate-through-collection-which-implements-ienumerable-only.aspx</guid>
            <pubDate>Fri, 12 Oct 2007 15:06:25 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Saqib/comments/118412.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Saqib/archive/2007/10/12/linq-iterate-through-collection-which-implements-ienumerable-only.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Saqib/comments/commentRss/118412.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Saqib/services/trackbacks/118412.aspx</trackback:ping>
        </item>
    </channel>
</rss>