I am going to access SharePoint list using LINQ and load to grid. I have Employees and Projects lists in share point site.
I've found some code here
private void BindGrid()
{
SPLinqDataContext dc = new SPLinqDataContext(SPContext.Current.Web.Url);
EntityList<EmployeesItem> Employees = dc.GetList<EmployeesItem>("Employees");
var EmployeeQuery = from e in Employees.ToList()
select new
{
e.Title,
e.FirstName,
Position = e.Position.Title,
PositionDescription = e.Position.Description,
Department = e.Position.Department.Title
};
GridView1.DataSource = EmployeeQuery;
GridView1.DataBind();
}
My problem is regarding <EmployeesItem>. Is this inherits with my reference? or should I suppose to create a separate class in .net as we normally do?
note - I mean reference is:
We should generate the LINQ to SharePoint proxy code in order to use LINQ.
spmetal.exe /web:http://localhost/sites/MySampleWebSite /namespace:AccessSPDatawithLINQ.VisualWebPart1 /code:SPLinq.cs
To my understanding
Point 1: You will execute your LINQ query and you will read complete SPList
Employeesfrom sharepoint toList<EmployeesItem>in memory. Now I don't know if you modify theseEmployeesItem, will that reflect in your SP list? You can test this yourself.Point 2: Here you are creating a Anonymous type. If you modify this object, it will not modify your
EmployeesItemobject.Point 3: Here you have in memory query, which has
IEnumerableof Anonymous type. Unless and until you evaluate thisIEnumerable(by doing.ToList()), yourEmployeeQuerywill not execute. As of now, according to your code, when you do.DataBind(); yourEmployeeQuerywill execute.}
Hope this helps.