LINQ to SQL – IsNull and Ordering

Saturday, January 30, 2010

I thought I would share a nice small tweak I made to a LINQ to SQL model. Assume I have the following table structure for ‘Categories’: I faced with the need to select categories while ordering it by the combination of Description1 and Description2 (Note: nullable fields). I entered some dummy data to the table to illustrate the point:   The goal is to retrieve the order as follows: 1, 3, 5, 2, 4 After explaining that, lets forward to the LINQ statements then. Take 1...
tags: , ,
4 comments

LINQ to XML – XElement – Descendants vs. Elements

Thursday, January 15, 2009

Someone asked me what the difference between descendants and elements is, I thought I'd post the details here for common interest.Descendants will yield you elements of your choice from the entire source element sub-tree, whereas Elements will yield the child elements only.E.g. string xml = @"                <Root>                    <Item>                        <id>1</id>                    </Item>                    <Item>                        <id>2</id>                    </Item>                    <Item>                        <id>3</id>                        <Items>                            <Item>                                <id>5</id>                            </Item>                            <Item>                                <id>6</id>                            </Item>                                                    </Items>                    </Item>                    <Item>                        <id>4</id>                    </Item>                </Root>";            XDocument doc1 = XDocument.Parse(xml);            var q1 = from e in doc1.Root.Descendants("Item")                     select e;            var q2 = from e in doc1.Root.Elements("Item")                     select e;            int c1 = q1.Count(); //6            int c2 = q2.Count();...
tags: ,
6 comments

LINQ to SQL Dynamic Mapping

Via this post. The post discusses the issue of using LINQ-to-SQL with dynamic mappings. The problemYou have SQL Servers for each of your environments (development, test, pre-prod, production) and in each one, the table names are different.  In fact, it’s not the table names that are different but the schema names but since the schema name is part of the table name, it must be specified.Ex : dev.ZeTable et prod.ZeTable Easy to solve, right?  Just put the schema value in the config file and do a simple concatenation at runtime.  Well, it’s not as easy as it seams because the table name...
tags: ,
no comments

LINQ To Regex

Via this post by Roy Osherove. A nice approach for working against regular expressions.I must admit, it looks as if it would take some time to master and love. Nice to know it's out there :)           public void FindEmailUsingPattern()         {             var query = from match in                RegexQuery.Against("sdlfjsfl43r3490r98*(*Email@somewhere.com_dakj3j")                         where match.Word.Repeat.AtLeast(1)                             .Literal("@")                             .Word.Repeat.AtLeast(1)                             .Literal(".")                             .Choice.Either(                                  Pattern.With.Literal("com"),                                  Pattern.With.Literal("net"))                             .IsTrue()                         select match;             foreach (var match in query)             {                 Assert.AreEqual("Email@somewhere.com",match.Value);             }         }
tags: ,
no comments

Batch Updated and Deletes with LINQ to SQL

A great post by Terry Aney. The post explains the gotchas when using LINQ to SQL to perform batch updates and deletes and offers an elegant and a very impressive solution! Worth checking it out.
tags: ,
no comments

LINQ to XSD Alpha 0.2

A new version of LINQ to XSD had been released last week. The new version works with the final release of VS 2008. "The LINQ to XSD preview illustrates our initial thinking on a strongly-typed programming experience over LINQ to XML. Instead of working with untyped XML trees, LINQ to XSD allows you to program in terms of strongly-typed classes, generated based on an XSD schema." Follow this link to get the details and download information.
tags: ,
2 comments