Sitecore: the call is ambiguous between the following methods or properties: "Sitecore.Data.ID.operator! = (Sitecore.Data.ID, Sitecore.Data.ID)"

For the following code: I get the error mentioned in the topic.

var subsidiariesIdList = new List<ID>();

MultilistField multilistField = newsItem.Fields["Recipient Subsidiaries"];
foreach (var item in multilistField)
{
    var data = Sitecore.Data.ID.Parse(item.ToString());

    if (data != null) // I am getting error here 
        subsidiariesIdList.Add(data);
}

Here is the full error text:

The call is ambiguous between the following methods or properties: "Sitecore.Data.ID.operator! = (Sitecore.Data.ID, Sitecore.Data.ID)" and "Sitecore.Data.ID.operator! = (Sitecore.Data.ID, Sitecore.Data.ShortID) '

+4
source share
3 answers

Just type null in ID:

if (data != (ID)null)

The reason you need to do the throw is because the class IDhas 2 statements:

public static bool operator ==(ID id1, ID id2)
public static bool operator ==(ID id1, ShortID id2)

null ID, ShortID. , .

+4

IsNullOrEmpty ID:

var subsidiariesIdList = new List<ID>();

MultilistField multilistField = newsItem.Fields["Recipient Subsidiaries"];
foreach (var item in multilistField)
{
    var data = Sitecore.Data.ID.Parse(item.ToString());

    if (!ID.IsNullOrEmpty(data))
        subsidiariesIdList.Add(data);
}
0

, , , foreach, Sitecore MultilistField , , foreach GetItems() '|' ?

/ Sitecore ID TryParse:

var subsidiariesIdList = new List<ID>();

    MultilistField multilistField = newsItem.Fields["Recipient Subsidiaries"];
    foreach (var item in multilistField)
{
ID id;
if (Sitecore.Data.ID.Parse(item.ToString(), out id))
{
    subsidiariesIdList.Add(id);
}

:

MultilistField multilistField = newsItem.Fields["Recipient Subsidiaries"];

if (multilistField != null && multilistField.Count > 0)
{
var subsidiariesIdList = multilistField.GetItems().Select(i=>i.ID).ToList<ID>();
}

Enjoy.

P.s. , iPhone.

-1

Source: https://habr.com/ru/post/1612596/


All Articles