built by Microsoft to make working with data ex Access record from Table by using object.
Example:
List<Contact> contacts = Contact.SampleData();
// perform the LINQ query line 2
var query = from c in contacts
orderby c.State, c.LastName
group c by c.State;
// write out the results
foreach (var group in query)
{
Console.WriteLine(”State: “ + group.Key);
foreach (Contact c in group)
Console.WriteLine(” {0} {1}”,
c.FirstName, c.LastName);
}
Here in example line 2 we can easily find out how Database table use as a object.
from c in contracts here contracts is a list which contain lot of record we are assemble all the record in variable c. means here c is used as a Object which contain the table data. c.state show the data field in contact list.( In linq all the quarry Write down just opposite to sql. The same quarey write down in sql as.
select state,lastname from contract
order by state group by LastName.
LINQ to Objects allows .NET developers to write “queries” over collections of objects.working with collections of objects meant writing a lot of looping code using for loops or foreach loops.
As simple example
int[] nums = new int[] { 0, 4, 2, 6, 3, 8, 3, 1 };
var result = from n in nums
where n < 5
orderby n
select n;
foreach (int i in result)
Console.WriteLine(i);
Output 2-3
0
1
2
3
3
4
Listing
Note----> Select-From-Where-OrderBy,(we follow this format when using SQL!!!!!!
From-Where-OrderBy-Select,(Just reverse in LINQ)
Query joins two collections
List<Contact> contacts = Contact.SampleData();
List<CallLog> callLog = CallLog.SampleData();
var q = from call in callLog
join contact in contacts on
call.Number equals contact.Phone
select new {
contact.FirstName,
contact.LastName,
call.When,
call.Duration
};
foreach (var c in q)
Console.WriteLine(
”{0} – {1} {2} ({3}min)”,
c.When.ToString(”ddMMM HH:m”), c.FirstName,
c.LastName, c.Duration);
Output 2-7
07Aug 08:
Here we are fetch all record from Table contacts and callLog(In which phone no field is common on behalf of this we are join both table and fetch data according to our requirement.
List<Contact> contacts = Contact.SampleData();
List<CallLog> callLog = CallLog.SampleData();
var q = from call in callLog
where call.Incoming == true
group call by call.Number into g
join contact in contacts on
g.Key equals contact.Phone
orderby contact.LastName, contact.FirstName
List<CallLog> callLog = CallLog.SampleData();
var q = from call in callLog
where call.Incoming == true
group call by call.Number into g
join contact in contacts on
g.Key equals contact.Phone
orderby contact.LastName, contact.FirstName
The Let keyword:
The let keyword enables you to keep the result of an expression
(a value or a subquery) in scope throughout the rest of the query expression
being written. Example
int [] values=new int [] {2,4,5,6,76,5};
var query = from i in values
let doublei = 2 * i
select doublei;
Console.WriteLine(query);
The new form of Linq
List<Contact> contacts = Contact.SampleData();
var q = contacts.Where(c => c.State == ”WA”)
.OrderBy(c => c.LastName)
.ThenBy(c => c.FirstName);
foreach (Contact c in q)
Console.WriteLine(”{0} {1}”,
c.FirstName, c.LastName);
var q = contacts.Where(c => c.State == ”WA”)
.OrderBy(c => c.LastName)
.ThenBy(c => c.FirstName);
foreach (Contact c in q)
Console.WriteLine(”{0} {1}”,
c.FirstName, c.LastName);
The simple form Linq as i am thinking
List<Contract> contacts= Contract.SampleData(); (here sample data just collect the ample data)
var q= from con in contacts
where con.state=='wa'
orderby con.LastName
select con;
Cosole.WriteLine(q);
The simple example how Linq work with sql joining
List<Contact> contacts = Contact.SampleData();
List<CallLog> callLog = CallLog.SampleData();
var q = (from call in callLog
join contact in contacts on
call.Number equals contact.Phone
orderby call.When descending
select new
{
contact.FirstName,
contact.LastName,
call.When,
call.Duration
}).Take(5);
foreach (var call in q)
Console.WriteLine(”{0} - {1} {2} ({3}min)”,
call.When.ToString(”ddMMM HH:m”),
call.FirstName, call.LastName, call.Duration);
List<CallLog> callLog = CallLog.SampleData();
var q = (from call in callLog
join contact in contacts on
call.Number equals contact.Phone
orderby call.When descending
select new
{
contact.FirstName,
contact.LastName,
call.When,
call.Duration
}).Take(5);
foreach (var call in q)
Console.WriteLine(”{0} - {1} {2} ({3}min)”,
call.When.ToString(”ddMMM HH:m”),
call.FirstName, call.LastName, call.Duration);
Where Filter Using a Lambda Expression
All lambda expressions use the lambda operator =>, which is read as "goes to".
use this link http://msdn.microsoft.com/en-us/library/bb397687.aspx
string[] animals = new string[] { ”Koala”, ”Kangaroo”,“Spider”, “Wombat”, “Snake”, “Emu”};
var q = animals.Where(
a => a.StartsWith(“S”) && a.Length > 5);
foreach (string s in q)
{
Console.WriteLine(s);
string[] animals = new string[] { ”Koala”, ”Kangaroo”,“Spider”, “Wombat”, “Snake”, “Emu”};
var q = animals.Where(
a => a.StartsWith(“S”) && a.Length > 5);
foreach (string s in q)
{
Console.WriteLine(s);
}
here a=> a.StartWith show d lambda experison.
here a=> a.StartWith show d lambda experison.
string[] animals = new string[] {"Koala", "Kangaroo" };
var q = from a in animal where a.StartsWith("K") && a.Length > select a;
foreach (string s in q)
Console.WriteLine(s);
var q = from a in animal where a.StartsWith("K") && a.Length > select a;
foreach (string s in q)
Console.WriteLine(s);
New form of linq(use function calling)
string[] animals = new string[] { ”Koala”, ”Kangaroo”,
”Spider”, “Wombat”, “Snake”, “Emu”, “Shark” };
var q = from a in animals
where IsAnimalDeadly(a) // in where clause call the function//
select a;
foreach (string s in q)
Console.WriteLine(“A {0} can be deadly.”, s);
public static bool IsAnimalDeadly(string s) //Here define the Method
{
string[] deadly = new string[] {“Spider”, “Snake”,
”Shark”, “Sting-Ray”, “Jellyfish”};
return deadly.Contains(s);
}
Output
A Spider can be deadly.
A Snake can be deadly.
A Shark can be deadly.
A Sting-Ray can be deadly.
A Jellyfish can be deadly.
Filtering by Index Position
The
zero-based index position can be passed into a lambda expression predicate
by assigning a variable name as the second argument.
To surface the index position, a lambda expression
must be used, and this can only be achieved using the extension method
syntax.
Example of Index position
string[] animals = new string[] { ”Koala”, ”Kangaroo”,
”Spider”, “Wombat”, “Snake”, “Emu”, “Shark” };
var q = from a in animals
where IsAnimalDeadly(a) // in where clause call the function//
select a;
foreach (string s in q)
Console.WriteLine(“A {0} can be deadly.”, s);
public static bool IsAnimalDeadly(string s) //Here define the Method
{
string[] deadly = new string[] {“Spider”, “Snake”,
”Shark”, “Sting-Ray”, “Jellyfish”};
return deadly.Contains(s);
}
Output
A Spider can be deadly.
A Snake can be deadly.
A Shark can be deadly.
A Sting-Ray can be deadly.
A Jellyfish can be deadly.
Filtering by Index Position
The
zero-based index position can be passed into a lambda expression predicate
by assigning a variable name as the second argument.
To surface the index position, a lambda expression
must be used, and this can only be achieved using the extension method
syntax.
Example of Index position
string[] animals = new string[] { "Koala", "Kangaroo",
"Spider", "Wombat", "Snake", "Emu", "Shark",
"Sting-Ray", "Jellyfish" };
var q = animals.Where((a, index) => index % 2 == 0);
foreach (string s in q)
Console.WriteLine(s);
"Spider", "Wombat", "Snake", "Emu", "Shark",
"Sting-Ray", "Jellyfish" };
var q = animals.Where((a, index) => index % 2 == 0);
foreach (string s in q)
Console.WriteLine(s);
No comments:
Post a Comment