簡體   English   中英

比較兩個列表中的元素時,LINQ查詢返回null

[英]LINQ query returning null when comparing elements in two lists

我正在檢查兩個列表中的元素,並嘗試添加到另一個列表中:

List<ProductPacking> prodSubstitute = new List<ProductPacking>();
tempList = new Dictionary<string, string>();
List<string> prodIDList = new List<string>();
for (int count = 0; count < prodSubstitute.Count; count++)
        {
            foreach (string key in tempList.Keys)
            {
                if (!prodSubstitute.Any(i => i.id == key))
                {
                    prodIDList.Add(prodSubstitute[count].id);
                }
            }
        }

假設我的prodSubstitute的測試ID是1,5,4,2,3。 tempList id中的元素是1,2,3。 當我通過prodSubstitute循環時,如果prodSubstititute不包含tempList中的ID,則應該將其添加到prodIDList中。 但是,使用此LINQ查詢,它只會使我返回null,而不是5,4。

有什么線索嗎? 提前致謝。

編輯

if (!prodIDList.Contains(prodSubstitute[count].id) && !(lstCategory.Where(x => x.Equals(categoryName)).Select(x => x).Count() >= 2))
                {
                    prodIDList.Add(prodSubstitute[count].id);
                    lstCategory.Add(categoryName);
                }

這樣的事情怎么樣:

var prodIDList = tempList.Keys.Except(prodSubstitute.Select(p => (string)p.id);

這應該產生什么之間的差異tempList ,什么是在prodSubstitute並把它轉換成字符串列表。

注意:這應該替換您當前擁有的所有代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM