本文介绍了从SPFieldLookupValueCollection中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个multilookup专栏Employee。如果某些特定条件为真,我想从列中删除一个值。以下是代码:



I have a multilookup column "Employee". i wanted to remove one value from the column if some specific condtion is true. Below is the code:

public override void ItemUpdated(SPItemEventProperties properties)
{
    base.ItemUpdated(properties);
    using (SPWeb web = properties.OpenWeb())
    {
        SPList TestA = web.Lists["TestA"];
        string employee = Convert.ToString(properties.ListItem["EmployeeNo"]);
        string department = Convert.ToString(properties.AfterProperties["Department"]);
        int ItemID = properties.ListItem.ID;
        SPList TestB = web.Lists["TestB"];
        SPQuery query = new SPQuery();
        query.Query = "<Where><Eq><FieldRef Name='Department' /><Value Type='Text'>" + department + "</Value></Eq></Where>";
        SPListItemCollection items = TestB.GetItems(query);
        if (items.Count > 0)
        {
            foreach (SPListItem Item in items)
            {
                web.AllowUnsafeUpdates = true;
                SPFieldLookupValueCollection objLookupFieldValueCol = (SPFieldLookupValueCollection)Item["Employee"];
                SPFieldLookupValue lookupValue = new SPFieldLookupValue();
                lookupValue.LookupId = ItemID;
                objLookupFieldValueCol.Remove(lookupValue);
                Item["Employee"] = objLookupFieldValueCol;
                Item.Update();
                web.AllowUnsafeUpdates = false;
            }
        }
        else
        {
            //SPListItem NewlistItem = TestB.AddItem();
            SPFieldLookupValue lookupValue = new SPFieldLookupValue();
            lookupValue.LookupId = ItemID;
            SPFieldLookupValueCollection objLookupFieldValueCol = new SPFieldLookupValueCollection();
            objLookupFieldValueCol.Remove(lookupValue);

            //NewlistItem["Employee"] = objLookupFieldValueCol;
            //NewlistItem["Department"] = department;

            //NewlistItem.Update();
        }
        web.AllowUnsafeUpdates = false;
    }
}



它没有从查询列中删除任何值。


it is not removing any value from the lookup column.

推荐答案

objLookupFieldValueCol.Remove(objLookupFieldValueCol.Find(delegate(SPFieldLookupValue LV){return LV.LookupId == ItemID;}));


这篇关于从SPFieldLookupValueCollection中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:36