Skip to main content

Posts

Showing posts with the label Linq

Compare Linq and Ruby Part1

Linq Where equivalent in RubyOne of the most used (just guessing) LINQ methods would be Where(), in .NET allows you to provide a Lambda that is used to filter a collection of objects var ints = new[] { 13, 42, 96, 2, 83 }; var evens = ints.Where(x = > x % 2 == 0); foreach(int i in evens) { Console.WriteLine(i); }And in Ruby this functionality would be achieved with select.ints = [13, 42, 96, 2, 83] evens = ints.select { | x | x % 2 == 0 } puts evensLinq OrderBy expression equivalent in RubyTo sort a sets of elements in c# with Linq we use OrderByvar words = new[] { "Perl", "c#", "ruby", "java" }; var ordered = words.OrderBy(x = > x); ordered.ToList().ForEach(Console.WriteLine);And in Ruby this functionality would be achieved with sort. words = ["Perl", "c#", "ruby", "java"] ordered = words.sort puts orderedLinq Select expression equivalent in RubyIn .NET to convert a set of items to anot…

How to read namespace based xml using Linq

In this post,I will show you how to read xml with namespace.For this demo,I will use following xmlstructure<?xml version="1.0" encoding="utf-8" ?> <accounts xmlns="urn:account"> <account>1001</account> <account>1002</account> </accounts> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace XLinq { class Program { static void Main(string[] args) { var doc = XDocument.Load("accounts.xml"); var query = from x in doc.Descendants("{urn:account}account") select x; foreach (var item in query) { Console.WriteLine(item); } } } }

Project Euler Problem4[C#]

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectEuler { class Program { staticvoid Main(string[] args) { Console.WriteLine(Problem4(600851475143UL)); } /// <summary>/// http://projecteuler.net/problem=3/// </summary>/// <param name="number"></param>/// <returns></returns>publicstaticulong Problem4(ulong number) { ulong i = 2; while (i * i < number) { while (number % i == 0) { number = number / i; } i = i + 1; } return number; } } }

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); …

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 bind Gridview using Linq (Join) and access the value of gridview cell

In this post i am going to show how to read the value of GridView cell when the DataSource is anonymous type.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %><!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></head><body><formid="form1"runat="server"><div></div><asp:GridViewID="GridView1"runat="server"OnRowDataBound="GridView1_RowDataBound"></asp:GridView></form></body></html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; publicpartialclass Default3 : System.Web.UI…

How To display Image using Linq To Sql

Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %><!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></head><body><formid="form1"runat="server"><div><asp:GridViewID="grdCategories"runat="server"DataKeyNames="CategoryID"Width="600px"AutoGenerateColumns="false"><Columns><asp:BoundFieldDataField="CategoryID"HeaderText="CategoryID"/><asp:BoundFieldDataField="CategoryName"HeaderText="CategoryName"/><asp:TemplateField><ItemTemplate><asp:ImageID="Image1"runat="server"Image…