Linq select
Select sample
---Posted from iPhone App---
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Datta KharadRank #2,135 · 668 reputationPosted Nov 15, 2011, 7:52 PM EST
Ex 1)
var obj= from n in Tablname or Collection
Select n where colid=12 order by colname;
--------------------------------------------------------------
Ex 2)
public void SimpleQuery()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
var q =
from a in dc.GetTable<Order>()
select a;
dataGridView1.DataSource = q;
}
-----------------------------------------------------------
Ex 3)
Understanding from..where..select
The from..where..select syntax in LINQ is similar to SELECT..FROM..WHERE clause of SQL. Here is a simple from..select.
from day in daysArray
select day;
In the above statement, I select day in daysArray. It returns an object of "var" type, which is a generic type and can store any type. For example, daysArray can be an array of numbers, strings, or even objects. Here is an example of daysArray.
string[] daysArray = { "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun" };
This is how return value is stored from a from..select statement.
var days = from day in daysArray select day;
Now where clause is used to filter the results. For example, the following syntax gets retuslts from daysArray where value of day is "Wed" only.
var days = from day in daysArray where day =="Wed" select day;
Filtering using where
The following code selects items from an array that have value below 20.
int[] IntArray = { 12, 5, 24, 10, 9, 8, 4, 87, 23, 7, 11, 43 };
var under20 = from u20 in IntArray where u20 < 20 select u20;
foreach (var numU20 in under20)
{
Console.WriteLine(numU20);
}
Please mark as Accepted answer if your query resolved.