LINQ

简单合计
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
double numSum = numbers.Sum();

 

合计数组中的字符数
string[] words = { "cherry", "apple", "blueberry" };
double totalChars = words.Sum(w => w.Length);

 

找到最小的值
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int minNum = numbers.Min();

 

找到字符数最少的那个串
string[] words = { "cherry", "apple", "blueberry" };
int shortestWord = words.Min(w => w.Length);


检查数组中是否有至少一个包含ei的值
string[] words = { "believe", "relief", "receipt", "field" };
bool iAfterE = words.Any(w => w.Contains("ei"));

 

取表中Name字段唯一值并且排序
DataTable orders = dataSet.Tables[0];
EnumerableRowCollection<DataRow> query =(from order in orders.AsEnumerable() orderby order.Field<string>("Name") select order.Field<string>("Name")).Distinct();
DataView view = query.AsDataView();
GridView1.DataSource=view;
GridView1.DataBind();

 

按表中Name字段分组,合计Money
DataTable orders = dataSet.Tables[0];
var query =from order in orders.AsEnumerable() group order by order.Field<string>("Name") into g select new { Names=g.Key,Moneys=g.Sum(order=>order.Field<decimal>("Money"))};
GridView1.DataSource = query.AsQueryable();
GridView1.DataBind();

 

内连接
DataTable orders = dataSet.Tables[0];
DataTable citys = dataSet.Tables[1];
var query =from order in orders.AsEnumerable()
           join city in citys.AsEnumerable()
           on order.Field<string>("Name") equals city.Field<string>("Name")
           select new
           {
               Name = order.Field<string>("Name"),
               Money = order.Field<decimal>("Money"),
               City = city.Field<string>("City")
           };

 

左连接
DataTable orders = dataSet.Tables[0];
DataTable citys = dataSet.Tables[1];
var query =from order in orders.AsEnumerable()
           join city in citys.AsEnumerable()
           on order.Field<string>("Name") equals city.Field<string>("Name") into cits
           select new
           {
               Name = order.Field<string>("Name"),
               Money = order.Field<decimal>("Money"),
               City = cits.Count() > 0 ? cits.First().Field<string>("City") : ""
           };

 

合并并且取掉重复项
int[] S1 = { 1, 2, 3, 4, 5 };
int[] S2 = { 3, 2, 7, 8, 10 };

var S3s = S1.Union(S2);
foreach (var S3 in S3s)
  MessageBox.Show(S3.ToString());


通过新建项LINQ TO SQL类,增加拖拽表TRANSTEST
带事务的修改记录
DataClassesDataContext db = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["TESTDBConnectionString"].ConnectionString);
db.CommandTimeout = 60;
using (TransactionScope ts = new TransactionScope())
{
  var records = db.TRANSTEST.Where(p => p.Name == "MAJ'IE");
  foreach (var record in records)
  {
    record.orders = 1;
    record.other = DateTime.Now.Second.ToString();
  }
  db.SubmitChanges();
  ts.Complete();
}


增加记录
DataClassesDataContext db = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["TESTDBConnectionString"].ConnectionString);
TRANSTEST record = new TRANSTEST();
record.other = "ILOVEYOU";
db.TRANSTEST.InsertOnSubmit(record);
db.SubmitChanges();

posted @ 2013-04-28 16:27  寒@鹏  阅读(199)  评论(0编辑  收藏  举报