I might be approaching this problem completely wrong, but I'm still a bit new to SP development and it's late in the evening.
EDIT: Yup, I'm thinking about this wrong. I just remembered that the SPListCollection is tied to the DB, and if I were to add records it won't work. I'm going to try a cross list query instead.
I have two SP lists that I need to query off one, get a list of items, then using those items ID's query another list to get it's "children" each parent can have multiple children.
I can get that to work fine, the kicker is I need to build a SPListItemCollection for use in other parts of the code. I'm trying to retrofit some stuff to work with this new look up process, and it's all based on SPListItemCollections. Here's a code snippet:
foreach (SPListItem i in NAICParents)
{
//query child slides
SPQuery qChildren = new SPQuery();
SPFieldLookupValue tmp = new SPFieldLookupValue(i.ID, i.Title);
qChildren.Query = "<Where><Eq><FieldRef Name=\"paNAICParent\" /><Value Type=\"Lookup\">" + tmp.ToString().Substring(tmp.ToString().IndexOf('#') + 1) + "</Value></Eq></Where>";
target = _web.Lists["paNAICUpdateSlides"];
SPListItemCollection children = target.GetItems(qChildren);
SPListItem temp = null;
foreach (SPL开发者_StackOverflowistItem l in children)
{
temp =itmAll2.Add();
temp = l;
temp.Update();
}
}
It dies at the temp = itmAll2.add() . I get a null reference error. Which makes sense, because I never instantiated the SPListItemCollection, but I can't get the code to let me instantiate that to blank.
Can I copy the structure of a list to instantiate a blank list? Or something like that?
Thanks,
I would not recommend trying to reuse an SPListItemCollection in your code. This can result in SPWeb objects being reopened behind the scenes.
Instead, I would use SPListItemCollection.GetDataTable to get a DataTable
that will hold your in memory object which you can then reuse across your code. Alternatively you could create Bean objects that would correspond to each item in the list.
Then, you can add or remove items to your in-memory objects without opening more database connections.
精彩评论