简体   繁体   English

无法从&#39;System.Collections.Generic.IEnumerable转换 <int> &#39;到&#39;System.Collections.Generic.IEnumerable <int?> “

[英]Cannot convert from 'System.Collections.Generic.IEnumerable<int>' to 'System.Collections.Generic.IEnumerable<int?>'

class first
{
    private int? firstID;
}

class second
{
    private int secondID;
    private int secondField;
}

public override Expression<Func<first, bool>> FirstFilter()
{
    Contex db = new Contex();
    List<second> list = (from p in db.second select p).ToList();

    return b => list.Select(p => p.secondID).Contains(b.firstID);
}

and I have error: 我有错误:

cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable' 无法从“ System.Collections.Generic.IEnumerable”转换为“ System.Collections.Generic.IEnumerable”

I have tried many different ways, but I just don't know how can I fix it. 我尝试了许多不同的方法,但是我不知道该如何解决。

Use this: 用这个:

list.Select(p => p.secondID).Cast<int?>().Contains(b.firstID);

You are getting the problem, because list.Select(p => p.secondID) will be an IEnumerable<int? 您遇到了问题,因为list.Select(p => p.secondID)将是IEnumerable<int? , but because firstID is int (non-nullable), overload resolution cannot determine a valid overload of Contains to call. ,但是因为firstID是int (不可为空),所以重载解析无法确定要调用的Contains的有效重载。 You cannot implicitly convert from IEnumerable<int?> to IEnumerable<int> . 您不能从IEnumerable<int?>隐式转换为IEnumerable<int> The Cast extension method works by casting each element to int. Cast扩展方法通过将每个元素都转换为int来工作。

As mentioned in another answer, you could also simply pass in a non-nullable int into Contains: 如另一个答案中所述,您还可以将一个不可为null的int传递给Contains:

list.Select(p => p.secondID).Contains(b.firstID ?? 0);

But, do be aware that this might not be want. 但是,请注意,这可能不是必需的。 If the first list contains 0 , and firstID is null, the result will be true, because you pass in 0 when the value is null.The Cast version of the expression returns false when firstID is null. 如果第一个列表包含0 ,并且firstID为null,则结果为true,因为在值为null时firstIDfirstID为null时,表达式的Cast版本返回false。

尝试提供一个默认值firstID如果是空:

return b => list.Select(p => p.secondID).Contains(b.firstID ?? 0);

无法隐式转换类型 'System.Collections.Generic.List<object> ' 到 'System.Collections.Generic.IEnumerable<int><div id="text_translate"><p> 我想构建一个 function,它接受一个非负整数和字符串的列表,并返回一个过滤掉字符串的新列表。</p><p> 与此类似:</p><pre> ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b"}) => {1, 2} ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", 0, 15}) => {1, 2, 0, 15}</pre><p> 为此,我执行了以下操作:(评论显示了最初的尝试)</p><pre> using System.Collections; using System.Collections.Generic; using System.Linq; public class ListFilterer { public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems) { foreach (var item in listOfItems) { if(item is string) { listOfItems.Remove(item); } } // return listOfItems; <- Original attempt return listOfItems.ToList(); } }</pre><p> 这给出了标题中的错误:</p><pre> src/Solution.cs(16,13): error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)</pre><p> 所以我添加了我认为是正确的转换.ToList()但它最终没有做任何事情并且仍然提供相同的错误。 我对此感到困惑,因为在搜索和查看我认为类似的问题后,我仍然没有找到正确的方法来转换它,并且由于我对 Linq 和 Enumerable 的经验不足,我不确定去哪里找。</p><p> 任何帮助将不胜感激,谢谢你的时间</p></div></int></object> - Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>

暂无
暂无

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

相关问题 TreeView:无法从&#39;System.Collections.Generic.IEnumerable转换 <string> &#39;到&#39;System.Collections.Generic.IEnumerable - TreeView: Cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable 无法从&#39;System.Collections.Generic.IEnumerable转换为System.Collections.Generic.IEnumerable <string> - Cannot convert from 'System.Collections.Generic.IEnumerable to System.Collections.Generic.IEnumerable<string> 无法从“ System.Collections.Generic.IEnumerable”转换为System.Collections.Generic.IEnumerable <string> - cannot convert from 'System.Collections.Generic.IEnumerable' to System.Collections.Generic.IEnumerable<string> 无法隐式转换类型 'System.Collections.Generic.List<object> ' 到 'System.Collections.Generic.IEnumerable<int><div id="text_translate"><p> 我想构建一个 function,它接受一个非负整数和字符串的列表,并返回一个过滤掉字符串的新列表。</p><p> 与此类似:</p><pre> ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b"}) => {1, 2} ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", 0, 15}) => {1, 2, 0, 15}</pre><p> 为此,我执行了以下操作:(评论显示了最初的尝试)</p><pre> using System.Collections; using System.Collections.Generic; using System.Linq; public class ListFilterer { public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems) { foreach (var item in listOfItems) { if(item is string) { listOfItems.Remove(item); } } // return listOfItems; <- Original attempt return listOfItems.ToList(); } }</pre><p> 这给出了标题中的错误:</p><pre> src/Solution.cs(16,13): error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)</pre><p> 所以我添加了我认为是正确的转换.ToList()但它最终没有做任何事情并且仍然提供相同的错误。 我对此感到困惑,因为在搜索和查看我认为类似的问题后,我仍然没有找到正确的方法来转换它,并且由于我对 Linq 和 Enumerable 的经验不足,我不确定去哪里找。</p><p> 任何帮助将不胜感激,谢谢你的时间</p></div></int></object> - Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int> 如何解决以下错误:无法从&#39;int&#39;转换为&#39;System.Collections.Generic.IEnumerable <int> ? - how to solve the following error: cannot convert from 'int' to 'System.Collections.Generic.IEnumerable<int>? 参数1:无法从&#39;System.Collections.Generic.IEnumerable转换 <AnonymousType#1> &#39;到&#39;int&#39; - Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'int' 无法从&#39;System.Collections.Generic.IEnumerable转换 <char> &#39;到&#39;字符串&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string' 无法从“System.Collections.Generic.IEnumerable”转换<string> &#39; 到 &#39;T&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'T' LINQ:无法将类型'System.Collections.Generic.IEnumerable <int>'隐式转换为'int' - LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int' 不能隐式转换类型“System.Collections.Generic.IEnumerable<Book> &quot;到&#39;&quot;int&quot; - cannot implicitly convert type "System.Collections.Generic.IEnumerable<Book>" to '"int"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM