English 中文(简体)
a. 对清单的电子数字? “未含蓄地转换MyClass。 用于系统的项目:聚合物
原标题:Copy an enumerable to a List ? "Cannot implicitly convert type MyClass.Items to System.Collections.Generic.List<MyClass.Items> "
  • 时间:2012-01-13 04:44:25
  •  标签:
  • c#
  • c#-4.0

这是一个不容置疑的问题。 因此,如果我错过这个问题,那是因为我不完全理解正在发生的情况。

Issue

Compile err或: Cannot implicitly convert type MyClass.Items to System.Collections.Generic.List

<><><><>>>>

我正在通过<代码>电子数字<>代码>(List)对<代码>进行校正,但我无法回复所希望的记录,因为其类型是<代码>。 清单(因为我的全称是通过<条码>/代码>。

我对这种方法的投入是<代码>List,但似乎暗中投到。 IE amountable when I use the OrderBy.

我阅读了我发现的所有东西,但似乎都oil了:

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

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

www.un.org/Depts/DGACM/index_spanish.htm 《问题法典》

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 F或getThisOne = false;

    f或each (var SingleItemToCheck in BestItemCandidate)
    {
        F或getThisOne = false;

        // ... test f或 ItemDesirability, truncated
        if (Desirability < 0)
        {
            F或getThisOne = true;
        }

        if (!F或getThisOne)
        {
            // we are just returning the first desirable row
            // because we are s或ted on desirability
            // and consuming code can only digest a single item.
            return SingleItemToCheck;  // !!! ARGH, compile err或 !!!
        }
    }

    // means we looped through everything and found nothing of interest
    return null;
}
最佳回答

SingleItemToCheck is a single item, it is not a list. It does not have a ToList() method available. Simply create a list with that single item.

return new List<Items> { SingleItemToCheck };

或者,如果你仅对一个项目重新表示兴趣,将这种方法的返回类型改为单纯的<代码>Items,完全删除该清单。

另一种写法是,特别是如果你仅对一个项目感兴趣的话,就是简单地将内心的逻辑重新定位为一种功能,然后写一个疑问。

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

否则,如果你绝对需要一份清单,但只需要一份清单,则考虑其中一个项目。

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

如果没有任何内容,这将是一个空洞的清单。 那么,你可以选择放弃,正如你目前的法典所做的那样,或回到空洞名单,让打电话者处理这一清单,而不是处理结果。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签