Skip to main content

Posts

CASE Statement Equivalent in LINQ

In this post i will show you how how to generate "Case statement" from linq.Let's say that you want to select rows from [Order Details] table in Northwind database something like this:  If the product UnitPrice is >10 then you want to display it as "High"If the product UnitPrice is <5 then you want to display it as "Low" otherwise "Average"
Our query should include the switch case  clause. Something like this:

SELECT
(
CASE
WHEN UnitPrice >10 THEN 'High'
WHEN UnitPrice <5 THEN 'Low'
ELSE 'Average'
END ) AS ProductPrice
FROM [Order Details]




using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { NorthwindDataContext dc = new NorthwindDataContext(); var query = from o in dc.Order_Details select new …

Linq and Sql "where in" Operator

In this post i will  show you how to generate "where in " clause in Linq to Sql. Imagine that we want to select rows from Products table in Northwind database where ProductId matches 3,4, 10. Our query should include the Where IN clause. Something like this:

SELECT *FROM Products WHERE ProductID in (3,4,10)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { //Create a list List<int> list = new List<int>(); //Add items in list list.AddRange(newint[] {3, 4, 10 }); NorthwindDataContext dc = new NorthwindDataContext(); //Query database var query = from product in dc.Products where list.Contains(product.ProductID) select product; Console.WriteLine(query); …

How to use Jquery fullcalender in asp.net

This snippet will show how how to use Jquery fullcalender in asp.net Open Microsoft Visual Studio .NET. In Visual C# .NET, create a new website  named Fullcalender.Add  following code to Default.aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FullCalender.aspx.cs" Inherits="FullCalender" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><scriptsrc="Scripts/jquery-1.4.1.js"type="text/javascript"></script>     <script src="Scripts/fullcalendar.js" type="text/javascript"></script>     <script src="Scripts/gcal.js" type="text/javascript"></script>     <link href="Styles/fullcalendar.css" rel="styleshe…

Linq and Reflection

This snippet will show how LINQ can be used with reflection to retrieve specific metadata about type that match a specified search criterion using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { Assembly assembly = Assembly.Load("System.Data, Version=3.5.0.0, Culture=neutral, " + "PublicKeyToken= b77a5c561934e089"); var Query = from type in assembly.GetTypes() where type.IsClass select type; foreach (var item in Query) { Console.WriteLine(item.Name); } Console.WriteLine("Press any key to exit ... "); Console.ReadKey(); } } }

How to extract number from string using Linq

Imagine you have a string("ABCDE99F-J74-12-89A") and you want to extract only number from the string. This snippet will show how to extract number from string. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { string aString = "ABCDE99F-J74-12-89A"; // Select only those characters that are numbers IEnumerable<char> stringQuery = from ch in aString where Char.IsDigit(ch) select ch; // Execute the query foreach (char c in stringQuery) Console.Write(c + " "); } } }

How to apply Left Outer join in Linq to Sql

In this post i am going to show you how to apply Left outer join in Linq to sql. Imagine that we want to apply left outer join in Products and Categories table in Northwind database Something like this:

SELECT [t1].[CategoryName] AS [CategoryId] FROM [dbo].[Products] AS [t0] LEFTOUTERJOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[Categor yID])
As an example i will take northwind Products and Categories table.Both these tables contain CategoryId as common field. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { NorthwindDataContext dc = new NorthwindDataContext(); var filteredQuery = from p in dc.Products join c in dc.Categories on p.CategoryID equals c.CategoryID into joinData from data in joinData.DefaultIfEmpty() …

How to use flag enuemeration in c#/asp.net

Imagine we have a User class, and one of the user properties is Permission. The Permission enum can be defined as follows: enum Permission { None = 0, Read = 2, Write = 4, ReadWrite = 8, All = Int16.MaxValue } As you can see -these options could be combined in several ways like an user can have Read and Write permission or user can have only Read permission.Most of us will create seprate properties in user class but efficient way to resolve this problem is use flag enumeration.flag enumeration is an instance of the enumeration type to store any combination of the values that are defined in the enumerator list.You create a bit flags enum by applying the System.FlagsAttribute attribute and defining the values appropriately so that AND, OR, NOT and XOR bitwise operations can be performed on them. [Flag] enum Permission { None = 0, Read = 2, Write = 4, ReadWrite = 8, All = Int16.MaxValue }…