Search This Blog

Monday, November 25, 2013

LINQ Query Expressions C# example

C# > LINQ Query Expressions

Language-Integrated Query (LINQ) is the name for a set of technologies of .NET Framework. This component adds data query  capabilities directly into the C# language.
LINQ expressions are like SQL statements  and can be used to extract and process data from arrays, classes, XML and databases.

Data Source - Query Expression - Execute

Methods
Examples

Example 1.  Fundamental query concepts

            // data source
            int[] int_array = new int[] { 5, 4, 2, 8, 10 };

            // query expression
            IEnumerable<int> intQuery =
                from int_v in int_array
                where int_v >= 5
                select int_v;

            // Execute the query
            foreach (int i in intQuery)
            {
                MessageBox.Show(i.ToString());
            } 
    
Example 2. Order by query

            IEnumerable<int> intQuery =

                from int_v in int_array
                where int_v >= 5
                orderby int_v ascending
                select int_v;

Example 3.
Implement RANK function


Example 4. IsNumeric

Example 5. Order By Key Dictionary


         





Friday, November 22, 2013

sp_columns SQL Server example

SQL Server > System Stored Procedures   > sp_columns

sp_columns returns column information for the specified object

Example

create table tmp_Sales(ProductId int, [Year] int, Country varchar(100))
EXEC sp_columns @table_name = N'tmp_Sales'
drop table tmp_Sales

Result:







PIVOT SQL Server Example

SQL Server  > Operators > PIVOT

PIVOT rotates a values from a table by turning the unique values from one column into multiple columns in the output (cross-tab). Also performs aggregations where they are required.

Example:

1.Using PIVOT to get number of product sales

create table #Sales(ProductId int, [Year] int, Country varchar(100))

insert into #Sales(ProductId, [Year], Country  ) values  (1,2012,'Germany'), (1,2012,'UK') , (1,2013,'France') , (2,2013,'Germany')  , (2,2013,'Germany')  , (2,2013,'UK')

SELECT
       Country, [2012], [2013]
FROM
       (SELECT ProductId, [Year], Country FROM #Sales) p
       PIVOT
       (
              COUNT (ProductId)
              FOR [Year] IN ( [2012], [2013] )
       ) AS pvt
ORDER BY pvt.Country ;

drop table #Sales

Result:



2. Dynamic PIVOT and cross-tab query






Create Local temporary table in SQL Server

SQL Server  > Temporary table > Create

Use # character in front of table name to create local temporary table

Example: Create temporary table #user

create table #user(id int, superiorid int)
insert into #user(id, superiorid)
values (1,null), (2,1) , (3,1)
drop table #user









Visual Basic run-time member functions

VB.NET  > Run-time member functions


DateAddDateDiffDatePartDateSerial
DateValueDayDDBDeleteSetting
Derived MathDirEnvironEOF
ErrorToStringFileAttrFileCloseFileCopy
FileDateTimeFileGetFileGetObjectFileLen
FileOpenFilePutFilePutObjectFileWidth
FilterFixFormatFormatCurrency
FormatDateTimeFormatNumberFormatPercentFreeFile
FVGetAllSettingsGetAttrGetChar
GetExceptionGetObjectGetSetting




What is Visual Basic .NET

Visual Basic .NET is the result of a significant rebuild of Visual Basic for the Microsoft .NET Framework.
Visual Basic .NET gives you access to system resources that in the past required the use of languages like C++. One of the most important additions is object inheritance.

RANK and DENSE_RANK SQL Server

SQL Server > Built-in Functions > RANK & DENSE_RANK

RANK returns the rank of each row within the partition of a result set

DENSE_RANK returns the rank of rows within the partition of a result set  without any gaps in the ranking

                    OVER ( [ PARTION BY ] [ORDER BY]) 
Example:

CREATE TABLE #LocalTempTable(
       ID            int,
       Name   varchar(50),
       Salary int,
       DeptId int)

insert into #LocalTempTable(Id, Name, Salary, DeptId) values (1,'p1',100,1),  (2,'p2',200,1) ,  (3,'p3', 100,1) ,   (2,'p4', 50,2)
 
--see image 1
select * from #LocalTempTable

--see image 2
SELECT i.*
    ,RANK() OVER
    ( ORDER BY i.Salary DESC) AS Rank
FROM
       #LocalTempTable i
ORDER BY i.DeptId;

--see image 3
SELECT i.*
    ,RANK() OVER
    (PARTITION BY i.DeptId  ORDER BY i.Salary DESC) AS Rank
FROM
       #LocalTempTable i
ORDER BY i.DeptId;

--see image 4
SELECT i.*
    ,DENSE_RANK() OVER
    ( ORDER BY i.Salary DESC) AS Rank
FROM
       #LocalTempTable i
ORDER BY i.DeptId;




drop table #LocalTempTable

Results:
 
Image 1 Salary Data
Image 2 RANK without PARTITION BY will rank all rows. Note the gap between 2 and 4
Image 3 RANK with PARTITION BY will rank rows in the same DeptID.
Image 2 DENSE_RANK  Note there are not gaps