简体   繁体   English

如何将IEnumerable <int>转换为Int32

[英]How do you convert IEnumerable<int> to an Int32

I've got a linq query that will return 1 result which will be an integer. 我有一个linq查询将返回1个结果,这将是一个整数。 I want to assign this to an Int32 variable to be used later, however I get an error that reads, "int does not contain a definition for RatingNumber and no extension method RatingNumber acepting a first argument of type int could be found (are you missing a using directive or an assembly reference?) 我想将它分配给一个Int32变量以便稍后使用,但是我得到一个错误,它读取“int不包含RatingNumber的定义而且没有扩展方法可以找到类型为int的第一个参数的RatingNumber(你是否遗漏了) using指令或程序集引用?)

this is the code that calls the query 这是调用查询的代码

IEnumerable<int> newRatingNumber = getNewRecipeNumbers.newRatingNum();

        foreach (var a in newRatingNumber)
        {
            ratingNumber = a.RatingNum;
        }

and this is the query: 这是查询:

 public IEnumerable<int> newRatingNum()
    {
        ratingTableAdapter.Fill(recipeDataSet.Rating);
        var newRatingNum = (from a in recipeDataSet.Rating
                            where a.UserRating == 0 &&
                            a.FamilyRating == 0 &&
                            a.HealthRating == 0 &&
                            a.EaseOfCooking == 0 &&
                            a.CookingTime == 0
                            select a.RatingNum);
        return newRatingNum;
    }

I tried using Convert.ToInt32 to cast the result to an int, this got rid of compiling errors, this however created an InvalidCastException. 我尝试使用Convert.ToInt32将结果转换为int,这消除了编译错误,但是这创建了一个InvalidCastException。 Anyone have any ideas? 有人有主意吗?

Thanks for the help 谢谢您的帮助

Craig 克雷格

The result of the Linq query is not a single Int value, but an IEnumerable. Linq查询的结果不是单个Int值,而是IEnumerable。 So you need to get a single value out of it, which in your case is the first value: 因此,您需要从中获取单个值,在您的情况下,这是第一个值:

var newRatingNum = (from a in recipeDataSet.Rating
                        where a.UserRating == 0 &&
                        a.FamilyRating == 0 &&
                        a.HealthRating == 0 &&
                        a.EaseOfCooking == 0 &&
                        a.CookingTime == 0
                        select a.RatingNum).FirstOrDefault();

FirstOrDefault() will return the first value in the Enumberable, or will return 0 if the Enumberable is empty. FirstOrDefault()将返回FirstOrDefault()的第一个值,如果Enumberable为空,则返回0。

If you have a List<Int32> , you theoretically have more than one. 如果你有一个List<Int32> ,理论上你有多个。 You can use First() , Last() , or Single() to pull a single element off of that list. 您可以使用First()Last()Single()从该列表中提取单个元素。 They all have OrDefault() versions which will return a 0 if the list is empty - otherwise they will error. 它们都有OrDefault()版本,如果列表为空则返回0 - 否则它们会出错。

Can't you use the Single or First methods to isolate a single Int32? 你不能使用Single或First方法隔离单个Int32吗? IEnumerable is implicitly many, when you need one. 当你需要时,IEnumerable隐含很多。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM