2012.7.8参照MSDN帮助学习了Linq的几个知识点,慢慢加深了对Linq的原理的理解,真心觉得 MSDN帮助才是经常该看的东西,而不是某些一知半解书店卖的书。
Linq 相当于ADO.NET 的sqlconnection
1. Foreach中的数据源必须继承自 IEnumberable<T> 或者派生接口Iquerable<T>
才可以用foreach (var a in 数据源)
自定义类,实现可以使用Foreach 循环。
1)类支持Ienumerable, 然后 public IEnumerator GetEnumberator() 返回每个子成员
class People : IEnumerable
{
private Person[] _people;
public People(Person[] pep)
{
_people = new Person[pep.Length];
for (int i = 0; i < pep.Length; i++)
{
_people[i] = pep[i];
}
}
public IEnumerator GetEnumberator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
2. Linq 延迟查询,即刻查询
默认延迟查询。
即刻查询: 查询语句加上 .ToList() 或者 .ToArray (或者后面直接紧接着foreach)
int[] numbers = new int[100];
for (int i = 1; i <= 100; i++)
{
numbers[i-1] = i;
}
//查询偶数-延迟查询
var queryFirst = from n in numbers
where (n % 2) == 0
select n;
foreach (var a in queryFirst)
{
Write(a);
}
//立即查询,并保存在结果集中 Tolist,toARRAY
List<int> querySeconde = (from n in numbers
where (n % 2) == 0
select n).ToList();
var queryThird = (from n in numbers
where (n % 2) == 0
select n).ToArray();
3. Linq 调用存储过程,执行4个基本操作(插入,删除,更新)
1) O/R设计器 ,创建映射到数据库表和视图的实体类
创建调用存储过程和函数的 DataContext 方法 (拖放存储过程到 O/R)
查看 xx.dbml 中的design.cs可以看见自动添加了 映射到 存储过程和函数的方法和相关类
[Function(Name="dbo.addWenJuan")]
public ISingleResult<addWenJuan_个结果> addWenJuan([Parameter(Name="WenJuanTitle", DbType="VarChar(200)")] string wenJuanTitle, [Parameter(Name="WenJuanContext", DbType="VarChar(8000)")] string wenJuanContext)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), wenJuanTitle, wenJuanContext);
return ((ISingleResult<addWenJuan_个结果>)(result.ReturnValue));
}
[Function(Name="dbo.f_split", IsComposable=true)]
public IQueryable<f_split_个结果> f_split([Parameter(Name="SourceSql", DbType="VarChar(8000)")] string sourceSql, [Parameter(Name="StrSeprate", DbType="VarChar(10)")] string strSeprate)
{
return this.CreateMethodCallQuery<f_split_个结果>(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sourceSql, strSeprate);
}
调用存储过程:
//自定义存储过程 CustomersByCityResult,并调用
ISingleResult<CustomersByCityResult_个结果> result = northwindDataContext.CustomersByCityResult("London");
foreach (var a in result)
{
Console.WriteLine("{0}-{1}-{2}-{3}",a.CustomerID,a.CompanyName,a.Contactname,a.city);
}
------------------------------------------------------------------------------------------
另外(与Linq 无关):可以根据现有数据表,通过数据字典自动创建存储过程。
项目名字--->新建项----》数据==》数据集 (.xsd)
双击项目中的XX.XSD文件--》
右击==》配置
然后就可以根据数据表自动创建好 4个存储过程,insert,update,select,delete
-------------------------------------------------------------------------------------------
4. Linq 更新,删除,修改,insert
//执行更新,插入,删除
Customers cus = (from c in northwindDataContext.Customers
select c).First();
Write("更改前name: "+cus.ContactName+"");
cus.ContactName = " dll";
Write("更改后name: " + cus.ContactName + ""); //此时只修改了内存中的值,数据库中的值并未更改
Orders ord = new Orders();
ord.OrderDate = DateTime.Now;
cus.Orders.Add(ord);
Orders ddd = cus.Orders[0]; //cus的第1个订单
//northwindDataContext.Orders.DeleteOnSubmit(ddd);
try
{
northwindDataContext.SubmitChanges();
}
catch (Exception exx)
{
Write(exx.Message);
}
finally
{
}
submitchanges()才生效。 deleteonsubmit 只是把状态变成deleted,但是没提交
直接执行sql命令 db.ExecuteCommand("UPDATE Products SET UnitPrice = UnitPrice + 1.00");
和ADO.NET共用sqlconnection,直接赋值相同sqltransaction