모든 LINQ 구문을 다 따라해보기에는 너무 많고 효율도 떨어질 것 같다.
기본적인 구문을 익히고 필요에 따른 Query 가 필요할 때 찾아보면 될 것 같다.
using System.Data.Linq; using System.Data.Linq.Mapping;
DataClasses1DataContext db = new DataClasses1DataContext();
db.Log = Console.Out;
var query = from c in db.tbOrgChart
where c.DEPTH == 3
select c;
dataGridView1.DataSource = query.ToList();
DataSet dataSet;
dataSet = new DataSet("Customers");
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True";
SqlDataAdapter adt = new SqlDataAdapter("select * from Customers", con);
adt.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
var query = from ord in dataSet.Tables[0].AsEnumerable()
where ord.Field("City") == textBox1.Text
select ord;
dataGridView1.DataSource = query.AsDataView();
namespace LINQtoXML
{
public class Customer
{
public string CustomerID { get; set; }
public string City { get; set; }
public override string ToString()
{
return CustomerID + "\t" + City;
}
}
}
var results = CreateXMLCustomers(); dataGridView1.DataSource = results.ToList();
var results = from c in CreateXMLCustomers()
where c.City == "London"
select c;
dataGridView1.DataSource = results.ToList();
/**
@brief XDocument, XElement을 이용한 XML 직접 쿼리
@note Load 메소드는 XDocument 클래스 타입을 리턴하며, XML 데이터 내용을 그대로 갖는다.
@note XDoment 객체를 LINQ 쿼리 결과 XElement<> 제네릭 컬렉션 객체를 얻게 된다.
하나의 XElement 는 하나의 레코드를 표현한다.
@note XML 을 쿼리할 때, XML 데이터를 담을 클래스를 미리 생성하여, 그 클래스를 쿼리
할 수도 있고, 이와 같이 직접 XML 데이터를 쿼리할 수도 있다.
*/
private void XMLQuery()
{
var doc = XDocument.Load("Customers.xml");
var results = from c in doc.Descendants("Customer")
where c.Attribute("City").Value == "London"
select c;
// dataGridView 에 출력
DataTable tblCustomers = new DataTable("tblCustomers");
dataGridView1.DataSource = tblCustomers;
DataColumn col = new DataColumn("CustomerID", typeof(string));
col.MaxLength = 10;
col.AllowDBNull = false;
col.Unique = true;
tblCustomers.Columns.Add(col);
tblCustomers.PrimaryKey = new DataColumn[] { col };
col = new DataColumn("City", typeof(string));
col.AllowDBNull = false;
tblCustomers.Columns.Add(col);
DataRow row;
foreach (var contact in results)
{
row = tblCustomers.NewRow();
row["CustomerID"] = contact.Attribute("CustomerID").Value;
row["City"] = contact.Attribute("City").Value;
tblCustomers.Rows.Add(row);
}
// 쿼리 결과로 새로운 XML 파일 생성
XElement transformedResults =
new XElement("Londoners", from customer in results
select new XElement("Contact",
new XAttribute("ID", customer.Attribute("CustomerID").Value),
new XElement("Name", customer.Attribute("ContactName").Value),
new XElement("City", customer.Attribute("City").Value)));
transformedResults.Save("Output.xml");
}
namespace LINQOverView
{
public class Customer
{
public string CustomerID { get; set; }
public string City { get; set; }
public override string ToString()
{
return CustomerID + "\t" + City;
}
}
}
public IEnumerableCreateCustomers() { return new List { new Customer { CustomerID = "ALFKI", City = "Berlin" }, new Customer { CustomerID = "BONAP", City = "Marseille" }, new Customer { CustomerID = "CONSH", City = "London" }, new Customer { CustomerID = "EASTC", City = "London" }, new Customer { CustomerID = "FRANS", City = "Torino" }, new Customer { CustomerID = "LONEP", City = "Portland" }, new Customer { CustomerID = "NORTS", City = "London" }, new Customer { CustomerID = "THEBI", City = "Portland" } }; }
var results = CreateCustomers(); dataGridView1.DataSource = results.ToList();
var results = from c in CreateCustomers()
where c.City == "London"
select c;
dataGridView1.DataSource = results.ToList();
IEnumerator ie = ht.Values.GetEnumerator();
while(ie.MoveNext())
{
Button b = (Button)ie.Current;
b.BackColor = Color.Blue;
}