简体   繁体   English

复制可枚举到列表? “无法将类型&#39;MyClass.Items&#39;隐式转换为&#39;System.Collections.Generic.List <MyClass.Items> “”

[英]Copy an enumerable to a List ? “Cannot implicitly convert type 'MyClass.Items' to 'System.Collections.Generic.List<MyClass.Items>'”

This is a noob question. 这是一个菜鸟问题。 So if I misstate the question, it's because I am not fully understanding what is happening. 因此,如果我错说了这个问题,那是因为我没有完全理解正在发生的事情。

Issue 问题

Compile error: Cannot implicitly convert type 'MyClass.Items' to 'System.Collections.Generic.List' 编译错误:无法将类型'MyClass.Items'隐式转换为'System.Collections.Generic.List'

Context 上下文

I am in iterating a List through an IOrderedEnumerable , but I am unable to return the desired record because it is of typed List (since my whole app is passing List objects back and forth). 我正在通过IOrderedEnumerable迭代List ,但是由于它是List类型的(因为我的整个应用程序来回传递List对象),所以我无法返回所需的记录。

My input to the method is a List , but it seems to be implicitly cast to IEnumerable when I use the OrderBy option. 我对该方法的输入是一个List ,但是当我使用OrderBy选项时,它似乎隐式转换为IEnumerable

I have read everything I've found, but it all seems to boil down to: 我已经阅读了所找到的所有内容,但似乎都可以归结为:

// ToList is not available!
return SingleItemToCheck.ToList;

or 要么

// same type conversion error
List<Items> ReturningList = SingleItemToCheck;

The Problem Code 问题代码

public static List<Items> FindCheapestItem(List<Items> source)
{
    // Now we pop only the first one. 
    // ISSUE: Sometimes the top entry on the list is bad, 
    //        so we need to check it!
    var BestItemCandidate = source.OrderBy(s => s.ItemDesirability);
    bool ForgetThisOne = false;

    foreach (var SingleItemToCheck in BestItemCandidate)
    {
        ForgetThisOne = false;

        // ... test for ItemDesirability, truncated
        if (Desirability < 0)
        {
            ForgetThisOne = true;
        }

        if (!ForgetThisOne)
        {
            // we are just returning the first desirable row
            // because we are sorted on desirability
            // and consuming code can only digest a single item.
            return SingleItemToCheck;  // !!! ARGH, compile error !!!
        }
    }

    // means we looped through everything and found nothing of interest
    return null;
}

SingleItemToCheck is a single item, it is not a list. SingleItemToCheck是单个项目,不是列表。 It does not have a ToList() method available. 它没有可用的ToList()方法。 Simply create a list with that single item. 只需使用该单个项目创建一个列表。

return new List<Items> { SingleItemToCheck };

Or if you're only ever interested in the one item, change the return type of the method to simply Items and omit the list entirely. 或者,如果您只对一项感兴趣,请将方法的返回类型更改为简单的Items然后完全忽略列表。

Another way to write that, particularly if you're only interested in the one item, is to simply refactor the inner loop logic to a function and then write a query 另一种写法,特别是如果您只对一项感兴趣的话,是简单地将内部循环逻辑重构为一个函数,然后编写查询

return source
         .OrderBy(s => s.ItemDesirability)
         .Where(s => IsDesirable(s)) // refactored loop logic, returning boolean
         .FirstOrDefault(); // first desirable item, or null

Otherwise, if you absolutely need a list, but only one item in it, consider 否则,如果您绝对需要一个列表,但列表中只有一个,请考虑

 var list =  source
         .OrderBy(s => s.ItemDesirability)
         .Where(s => IsDesirable(s))
         .Take(1)
         .ToList();

If no elements pass, this will be an empty list. 如果没有任何元素通过,这将是一个空列表。 You can then elect to return null, as your present code does, or return the empty list and let the callers deal with that instead of the null result. 然后,您可以选择像当前代码一样返回null,或者返回空列表,然后让调用者处理该列表而不是null结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.Collections.Generic.List' to 'string' 无法隐式转换类型System.Collections.Generic.List <char> - cannot implicitly convert type System.Collections.Generic.List<char> 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 无法隐式转换类型&#39;System.Collections.Generic.List错误 - Cannot implicitly convert type 'System.Collections.Generic.List error 无法隐式转换类型&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;&#39; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM