之前在查询list的时候,一般用caml来查,用U2UCaml拼caml query,稍微复杂点,就很头疼,最近尝试用Linq来查询,方便了许多,列些简单的例子
之前在查询list的时候,一般用caml来查,用U2UCaml拼caml query,稍微复杂点,就很头疼,最近尝试用Linq来查询,方便了许多,列些简单的例子
Linq查询List
List中查询SPListItem
List<SPListItem> item = (from f in listitems.OfType<SPListItem>() where f["DocItemGUID"] != null && f["DocItemGUID"].ToString() == _guid select f).ToList<SPListItem>();
感觉其中还是有点区别的,比如查Lookup字段
query是:
Query
<Query>
<Where>
<Eq>
<FieldRef Name="ParentNode" />
<Value Type="Lookup">Node1</Value>
</Eq>
</Where>
</Query>
而Linq则是:
Code
List<SPListItem> _list = listitem().FindAll(delegate(SPListItem _item) { return _item["ParentNode"] != null && _item["ParentNode"].ToString() == "1;#Node1"; });
List<SPListItem> _list = listitem().FindAll(delegate(SPListItem _item) { return _item["ParentNode"] != null && _item["ParentNode"].ToString() == item.ID.ToString() + ";#" + item.Title; });
需要ID加Name拼字符串
Linq查询UserProfile
查询UserProfile
List<UserProfile> listuserprofile = (from f in Pump.OfType<UserProfile>() where f["EmployeeID"] != null && f["EmployeeID"].Value != null && f["EmployeeID"].Value.ToString() == _EmployeeID select f).ToList<UserProfile>();