int[] numbers = new int[] { 10, 55, 43, 13, 57, 40, 22, 88 };
var result =
from number in numbers
where number % 2 == 0
orderby number descending
select number;
foreach (var item in result)
{
Console.Write("{0}\t", item);
}
------------------------------------------------------------------------
string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };
var groups =
from word in words
orderby word ascending
group word by word.Length into lengthGroup
orderby lengthGroup.Key descending
select new { Length = lengthGroup.Key, Words = lengthGroup };
foreach (var group in groups)
{
Console.WriteLine("Words of length " + group.Length);
foreach (string word in group.Words)
Console.WriteLine(" " + word);
}
------------------------------------------------------------------------
string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };
var shortWords =
from word in words
where word.Length <= 5
select word;
foreach (var word in shortWords)
Console.WriteLine(word);
------------------------------------------------------------------------
DataContext dataContext = new DataContext(@"Data Source=......");
Table<T_IC_APP> appsTable = dataContext.GetTable<T_IC_APP>();
var apps =
from app in appsTable
where app.APP_NAME.Contains("FF")
select app;
Console.WriteLine(dataContext.GetCommand(apps).CommandText);
foreach (var item in apps)
{
Console.WriteLine("APP_ID: {0}, APP_NAME: {1}, APP_DESC: {2}",
item.APP_ID, item.APP_NAME, item.APP_DESC);
}
[Table(Name = "T_IC_APP")]
public class T_IC_APP
{
[Column(IsPrimaryKey = true, Name = "APP_ID")]
public string APP_ID { get; set; }
[Column(Name = "APP_NAME")]
public string APP_NAME { get; set; }
[Column(Name = "APP_DESC")]
public string APP_DESC { get; set; }
}
------------------------------------------------------------------------
Book[] books = new Book[]
{
new Book("Ajax in Action", "Manning", 2005),
new Book("Windows Forms in Action", "Manning", 2006),
new Book("RSS and Atom in Action", "Manning", 2006)
};
XElement xml = new XElement("books",
from book in books
where book.Year == 2006
select new XElement("book",
new XAttribute("title", book.Title),
new XElement("publisher", book.Publisher)
)
);
Console.WriteLine(xml);
}
class Book
{
public string Publisher;
public string Title;
public int Year;
public Book(string title, string publisher, int year)
{
Title = title;
Publisher = publisher;
Year = year;
}
}