Vincent Leung .NET Tech Clips

The latest tech clips from the .NET community

Lucene tutorial

November 3, 2009 Posted by Vincent Leung | Database, LINQ, Tools | | No Comments Yet

LINQ: Building an IQueryable provider series

October 6, 2009 Posted by Vincent Leung | LINQ | | No Comments Yet

Moq with Linq Query Expression

 

 Before

 

image

After

image

Yes, it’s gonna be largely a matter of personal style and preference, just like many prefer invoking .Where(…) and .Select(…) instead of from…where…select.

Via Daniel Cazzulino’s Blog : Linq to Mocks is finally born

August 14, 2009 Posted by Vincent Leung | ASP.NET, LINQ, Mock, TDD | | No Comments Yet

LINQ to FQL (Facebook Query Language)

 

LINQ to FQL is an open source project, sources, binaries and samples can be found at CodePlex. This library extends the Facebook Developer Toolkit.

Introduction

This library enables developers to use LINQ (.NET Language-Integrated Query) with Facebook instead of string based FQL queries. The main benefits for using this library are:

  • Type safety: queries are now typed and their syntax is verified during compilation.
  • Auto-complete now function when composing queries in Visual Studio.
  • Using Facebook / anonymous objects for query results.

Example: this following sample code retrieves the names and pictures of all of your friends.

var db = new FacebookDataContext();
var friendIDs = from friend in db.friend_info where friend.uid1 == db.uid select friend.uid2;
var friendDetails = from user in db.user where friendIDs.Contains(user.uid) select new { Name = user.name, Picture = user.pic_small };

Via CodeProject: LINQ to FQL (Facebook Query Language). Free source code and programming help

March 9, 2009 Posted by Vincent Leung | Facebook, LINQ | | No Comments Yet

LINQ “Any” Keyword to check for existence of an element/attribute

One of the things you must do when writing a LINQ to Xml query definition is check for the existence of nonrequired elements and attributes before using them. For instance, a client application displays an image of the book in the ListBox control. The source URL of the image is grabbed from the SmallImage element’s URL element. If the SmallImage element does not exist, which is certainly a possibility if there is no image on record, the LINQ query definition must account for this.

For example,

<Item>
    <SmallImage>
        <URL>

http://www.silverlight-data.com/images/someImage.png

        </URL>
    </SmallImage>
</Item>

 

Before grabbing a value, you can perform a simple check to make sure the element exists; you do this by checking the Elements collection’s Any method. The Any method returns a Boolean value that indicates whether the collection contains elements. If the element exists, you can grab the value of the element. If the element does not exist you can set alternatives. The following shows the logic that checks for the existence of the image’s URL; if it is found it grabs the value and sets it to the imageSource variable.

let imageSource = (book.Elements(ns + "SmallImage").Any()
    ? book.Element(ns + "SmallImage").Element(ns + "URL").Value
    : string.Empty)
p.s. for more on the usage of the keyword let, take a look at http://vincenthomedev.wordpress.com/2007/12/19/linq-let-keyword/
 

February 22, 2009 Posted by Vincent Leung | LINQ | | No Comments Yet

LINQ Dynamic Query – Strongly Typed Way

 

In .NET 3.5 there is a System.Linq.Dynamic namespace which allows you to use string expressions in LINQ queries. It can be used to create dynamic queries but I don’t like it because it is not type-safe. So let me give you an introduction to anonymous functions, lambda expression trees and the PredicateBuilder class. These features can be used to create finder methods which can use dynamic strong-typed where-clauses as input parameters.

Via SCIP.be – Articles : .NET – LINQ to SQL – part 4

December 15, 2008 Posted by Vincent Leung | LINQ | | No Comments Yet

LINQ Dynamic Query Library

 

Example

Instead of using language operators or type-safe lambda extension methods to construct your LINQ queries, the dynamic query library provides you with string based extension methods that you can pass any string expression into.

For example,

Using the LINQ DynamicQuery library I could re-write the above query expression instead like so:

Notice how the conditional-where clause and sort-orderby clause now take string expressions instead of code expressions.  Because they are late-bound strings I can dynamically construct them.  For example: I could provide UI to an end-user business analyst using my application that enables them to construct queries on their own (including arbitrary conditional clauses).

Download Dynamic Query Library & Documentation

C# Dynamic Query Library (included in the \LinqSamples\DynamicQuery directory)

You can copy/paste either the C# or VB implementations of the DynamicQuery library into your own projects and then use it where appropriate to more dynamically construct LINQ queries based on end-user input.

Via Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library) – ScottGu’s Blog

December 15, 2008 Posted by Vincent Leung | LINQ | | No Comments Yet

Entity framework Learning Guid by Zeeshan Hirani

December 8, 2008 Posted by Vincent Leung | LINQ | | No Comments Yet

A List of LINQ Providers

November 29, 2008 Posted by Vincent Leung | LINQ | | 1 Comment

LINQ to SQL vs LINQ to Entities

 

When Do I Use LINQ to SQL?

The primary scenario for using LINQ to SQL is when building applications with a rapid development cycle and a simple one-to-one object to relational mapping against the Microsoft SQL Server family of databases. In other words, when building an application whose object model is structured very similarly to the existing database structure, or when a database for the application does not yet exist and there is no predisposition against creating a database schema that mirrors the object model; you can use LINQ to SQL to map a subset of tables directly to classes, with the required columns from each table represented as properties on the corresponding class. Usually in these scenarios, the database has not and/or will not be heavily normalized.

image

When do I use LINQ to Entities?

The primary scenario targeted by LINQ to Entities is a flexible and more complex mapping scenario, often seen in the enterprise, where the application is accessing data stored in Microsoft SQL Server or other-third party databases.

In other words, the database in these scenarios contains a physical data structure that could be significantly different from what you expect your object model to look like. Often in these scenarios, the database is not owned or controlled by the application developer(s), but rather owned by a DBA or other third party, possibly preventing application developers from making any changes to the database and requiring them to adapt quickly to database changes that they may not have been aware of.

image

Via Introducing LINQ to Relational Data

March 12, 2008 Posted by Vincent Leung | LINQ | | 1 Comment