"Select" is a query operator , Select operator returns result of a given query.
LINQ uses "select" query operator in two type, "select" and "select new". Select operator filter the original object, it is reference to the original objects. "Select new" also is query operator, that returns result of anonymous type by creating new instance of the particular type.
We are going to discuss about implementation of select statement in various type and designs.
1) The example simple select statement.
using System;
LINQ uses "select" query operator in two type, "select" and "select new". Select operator filter the original object, it is reference to the original objects. "Select new" also is query operator, that returns result of anonymous type by creating new instance of the particular type.
We are going to discuss about implementation of select statement in various type and designs.
1) The example simple select statement.
using System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Text;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add
(new
student
{name = "John1",
roll = 121,age = 5});
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 17 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 18 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 19 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 20 });
var
query = from
student
s in
myList
select
s;
foreach
(student
c in
query)
{
Console.WriteLine("Name
: "
+ c.name + "|Roll
:"
+ c.roll + "|
c.age:"
+ c.age);
}
Console.ReadLine();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Output :
Name : John1|Roll :121| c.age:5
Name : John2|Roll :122| c.age:6
Name : John3|Roll :123| c.age:7
Name : John4|Roll :124| c.age:8
Name : John5|Roll :125| c.age:9
Name : John6|Roll :126| c.age:10
Name : John7|Roll :127| c.age:11
Name : John8|Roll :128| c.age:17
Name : John9|Roll :129| c.age:18
Name : John10|Roll :1210| c.age:19
Name : John11|Roll :1211| c.age:20
Name : John2|Roll :122| c.age:6
Name : John3|Roll :123| c.age:7
Name : John4|Roll :124| c.age:8
Name : John5|Roll :125| c.age:9
Name : John6|Roll :126| c.age:10
Name : John7|Roll :127| c.age:11
Name : John8|Roll :128| c.age:17
Name : John9|Roll :129| c.age:18
Name : John10|Roll :1210| c.age:19
Name : John11|Roll :1211| c.age:20
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add(new
student
{ name = "John1",
roll = 121, age = 5 });
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 12 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 13 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 14 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 15 });
var
query = from
student
s in
myList
select
new
{
name
= s.name.ToUpper(),
roll
= s.roll ,
age
= s.age
};
Console.ReadKey();
}
}
}
Output :
Output :
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
3)The example shows that , select statement can be calculated in the output. We can perform some calculation during the output results.
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add(new
student
{ name = "John1",
roll = 121, age = 5 });
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 12 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 13 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 14 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 15 });
var
query = from
student
s in
myList
select
new
{
name
= s.name,
roll
= s.roll ,
age_in_month
= (s.age*12)
};
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
4)The examples show that you can apply if,iif on LINQ output.
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add(new
student
{ name = "John1",
roll = 121, age = 5 });
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 12 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 13 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 14 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 15 });
var
query = from
student
s in
myList
select
new
{
name
= s.name,
roll
= s.roll ,
Age_Group
= (
s.roll
< 10 ? "Child"
:
s.roll
< 15 ? "Teenage"
:
s.roll
< 18 ? "Adult"
: "Old
Age"
)
};
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
5)The examples show that you can apply method on LINQ output.
5)The examples show that you can apply method on LINQ output.
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add(new
student
{ name = "John1",
roll = 121, age = 5 });
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 12 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 13 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 14 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 15 });
Utility
u = new
Utility();
var
query = from
student
s in
myList
select
new
{
name
= s.name,
roll
= s.roll,
Age_Group
= u.GetAgeGroup(s.age).ToString()
};
foreach
(object
c in
query)
{
Console.WriteLine(c.ToString());
}
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
public
class
Utility
{
public
string
GetAgeGroup(int
cid)
{
return
cid < 10 ? "Child"
:
cid
< 15 ? "Teenage"
:
cid
< 18 ? "Adult"
: "Old
Age";
}
}
{ name = John1, roll = 121, Age_Group = Child }
{ name = John2, roll = 122, Age_Group = Child }
{ name = John3, roll = 123, Age_Group = Child }
{ name = John4, roll = 124, Age_Group = Child }
{ name = John5, roll = 125, Age_Group = Child }
{ name = John6, roll = 126, Age_Group = Teenage }
{ name = John7, roll = 127, Age_Group = Teenage }
{ name = John8, roll = 128, Age_Group = Teenage }
{ name = John9, roll = 129, Age_Group = Teenage }
{ name = John10, roll = 1210, Age_Group = Teenage }
{ name = John11, roll = 1211, Age_Group = Adult }
No comments:
Post a Comment