Friday, March 19, 2010

Simple LINQ Examples


Linq command


output


“shashi”


shashi


"YES!!!".Dump ("Foreign
key constraints"
);



Foreign key constraints


YES!!!


from n in new[]
{
"Tom", "Dick", "Harry" }

where n.Contains ("a")

select n



5IEnumerable<String>
(1 item)


Harry


from word in "The
quick brown fox jumps over the lazy dog"
.Split()

orderby word.Length

select word



5IOrderedEnumerable<String>
(9 items)


The


fox


the


dog


over


lazy


quick


brown


jumps


var words =

    
from
word
in "The
quick brown fox jumps over the lazy dog"
.Split()

    
orderby
word.ToUpper()

    
select
word;

    

var duplicates =

    
from
word
in words

    
group
word.ToUpper()
by word.ToUpper() into g

    
where
g.Count() >
1

    
select new {
g.Key, Count = g.Count() };



5IOrderedEnumerable<String>
(9 items)


brown


dog


fox


jumps


lazy


over


quick


The


the


5IEnumerable<>
(1 item)


Key


Count


THE


2




123.Dump();

Regex.Match (
"my favorite color is...", "colou?r").Dump();

formatting:

TimeZoneInfo.Local.Dump (
"Bet you never knew this type
existed!"
);







new[] { 11, 5, 17, 7, 13
}    .Dump (
"Prime
numbers"
)

.Where (n => n >
10)        .Dump
(
"Prime numbers > 10")

.OrderBy (n =>
n)            .Dump
(
"Prime numbers > 10 sorted")

.Select (n => n *
10)
        .Dump (
"Prime
numbers > 10 sorted, times 10!"
);

123


5Match


color


Groups



5GroupCollection
(1 item)



qMatch


color


Success


True


Captures



5CaptureCollection
(1 item)



qMatch


color


Index


12


Length


5


Value


color


▪ Bet you never knew
this type existed!



5TimeZoneInfo


(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi


Id


India Standard Time


DisplayName


(GMT+05:30) Chennai, Kolkata, Mumbai,
New Delhi


StandardName


India Standard Time


DaylightName


India Daylight Time


BaseUtcOffset


05:30:00


SupportsDaylightSavingTime


False

 


▪ Prime numbers



5Int32[] (5
items)


11


5


17


7


13

 


▪ Prime numbers >
10



5IEnumerable<Int32>
(3 items)


11


17


13

 


▪ Prime numbers >
10 sorted



5IOrderedEnumerable<Int32>
(3 items)


11


13


17

 


▪ Prime numbers >
10 sorted, times 10!



5IEnumerable<Int32>
(3 items)


110


130


170


(new[] {"Tom", "Dick", "Harry"}
).Where (n => n.Length >=
4)



5IEnumerable<String>
(2 items)


Dick


Harry


 


 


No comments:

Post a Comment