Linq To Sharepoint (ZT)
From http://blog.csdn.net/xylinzai_xy/article/details/6442484
linq to sharepoint和linq to sql是不完全一样的,其中就包括linq to sharepoint 不支持query多个list. MSDN中表明 join() 操作是一种效率低下的查找操作,为了避免低效率的linq query in sharepoint ,我们可以用linq to sharepoint provider 和linq to objects provider.这两种providers来解决低效率的linq query
接下来是实例讲解如何在sharepoint 中用linq 的join操作来连接查询
假设在我们的站点中有2个list,结构如下:
ProjectList {ProjectID , ProjectManger,ProjectPeriod}
ProjectDetail {ProjectID, ProjectName}
这两个list是随便建立的它们之前通过ProjectID关联,最终我们要的结果是包含四个字段的一个集合
Object {ProjectID,ProjectName,ProjectManger,ProjectPeriod}
一、使用spmetal.exe 生成我们需要的linq to sharepoint 代理类
语法:spmetal /web:http://ip:port /namespace:tess /code:linqHelper.cs
二、 将生成的linqHelper.cs添加到项目中,并且添加引用Microsoft.SharePoint.Linq;
三、 编码
var dc = new LinqHelperDataContext(SPContext.Current.Web.Url);
var Projects = dc.GetList<Project>("ProjectList");
var Pnames = dc.GetList<ProjectDetailItem>("ProjectDetail");
接下来我们或许会这么写:
Var queryResult=from c in Projects join d in Pnames on c.ProjectID equals d. ProjectID
Select new {
c.ProjectID,
c.ProjectManagerName,
d.ProjectName,
c.ProjectPeriod
}
编译程序不会报错,但是当我们部署后,运行则报错!
linq to sharepoint 不支持多个list的连接查询
正确的做法:
把其中一个EntityList转变成generic list
假设我现在要把Pnames转换成 genericlist
1、 New一个实体类,该实体类的属性和ProjectDetail中的column对应
Public class TempProject
{
Public int32 ProjectID{get;set;}
Public string ProjectName{get;set;}
}
2、 接着上面的写:
var dc = new LinqHelperDataContext(SPContext.Current.Web.Url);
var Projects = dc.GetList<Project>("ProjectList");
var Pnames = dc.GetList<ProjectDetailItem>("ProjectDetail");
var details=from d in Pnames select d;
var p_details=new list<TempProject>();
foreach(var I in details)
{
p_details.Add(new TempProject (d.ProjectID, d.ProjectName));
}
var queryResult = from c in Projects
join cd in p_details
on c.ProjectID equals cd.ProjectID
select new
{
c.ProjectID,
c.ProjectManagerImnName,
cd.ProjectName,
c.ProjectPeriod
};
3、 绑定数据到spgridview
this.SPGridView1.DataSource = queryResult;
this.SPGridView1.DataBind();
注:我们也可以把2个Entitylist都转换成generic list,然后进行join 操作
---------------------------------------------------------
Another example
Good example
0 Comments:
Post a Comment
<< Home