0

次のコードがあります。

foreach (SPListItem item in list.Items)
{

    string itemId = item.ID.ToString();
    string contentType = item.ContentType.ToString();
    string displayName = item.DisplayName;
    string name = item.Name;

    // todo: Title not always retreiving? Likely due to folders, needs more handling
    //string title = item["Title"].ToString() ?? null;
    string title = "";

    string url = item.Url;
    string author = item["Created By"].ToString();

    // todo: Checked out files catering
    const string checkedOutBy = "";

    DateTime lastModified = Convert.ToDateTime(item["Modified"]);
    string lastModifiedBy = item["Modified By"].ToString();
    DateTime created = Convert.ToDateTime(item["Created"]);


    query.RecordItems(itemId, contentType,
                        displayName, name,
                        title, url, author,
                        checkedOutBy,
                        lastModified,
                        lastModifiedBy,
                        created,
                        author);
}

私が抱えている問題は、ループの一部の反復で Title または ContentType が Nullreferenceexception をスローすることですが、すべてではありません。私は次の方法でこれに対応したと信じていますが、よくわかりません-より良い方法はありますか?

foreach (SPListItem item in list.Items)
{
    try
    {
        string itemId = item.ID.ToString();
        string contentType = item.ContentType.ToString();
        string displayName = item.DisplayName;
        string name = item.Name;

        // todo: Title not always retreiving? Likely due to folders, needs more handling
        //string title = item["Title"].ToString() ?? null;
        string title = "";

        string url = item.Url;
        string author = item["Created By"].ToString();

        // todo: Checked out files catering
        const string checkedOutBy = "";

        DateTime lastModified = Convert.ToDateTime(item["Modified"]);
        string lastModifiedBy = item["Modified By"].ToString();
        DateTime created = Convert.ToDateTime(item["Created"]);


        query.RecordItems(itemId, contentType,
                            displayName, name,
                            title, url, author,
                            checkedOutBy,
                            lastModified,
                            lastModifiedBy,
                            created,
                            author);
    }
    catch (NullReferenceException ex)
    {
        Logger.Error("[{0}] Filed moving on file {1} as not all content was present", item.Name);
    }

}
4

2 に答える 2

2

ここで問題を本当に理解していることを願っています。

foreach (SPListItem item in list.Items)
{
   ...... 
   if(title == null || item.ContentType == null) 
       continue;


   ....
}

これがあなたが求めているものでない場合は、明確にしてください。

于 2013-09-30T06:36:18.020 に答える
1

アイテムをフィルタリングして、以下のようにループできます

list = list.Items.Where(i=> i.ContentType !=null && i["Title"] !=null).ToList();

上記のすべての検証とフィルターを実行する必要がない場合は、に移動することをお勧めしますfor loop

for (int i = 0; i < list.Items.Count; i++)
{
    try
    {
       SPListItem item = list.Items[i];
       // your code 
    }
    catch (NullReferenceException ex)
    {
        Logger.Error("[{0}] Filed moving on file {1} as not all content was present", item.Name);
    }

}
于 2013-09-30T06:42:38.697 に答える