C# 将 LINQ 查询结果转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11338902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:30:08  来源:igfitidea点击:

converting LINQ query result to int

c#linqtype-conversion

提问by Kabi

I've written a LINQ query that should return an int value. but I'm not able to convert this value to int and an exception occured: Unable to cast object of type 'QuickRoutes.DAL.RouteLinq' to type 'System.IConvertible'.
this is my LINQ :

我编写了一个应该返回 int 值的 LINQ 查询。但我无法将此值转换为 int 并且发生异常: 无法将类型为“QuickRoutes.DAL.RouteLinq”的对象转换为类型“System.IConvertible”。
这是我的 LINQ:

var res = (from p in aspdb.RouteLinqs
                       orderby p.RouteId descending
                       select p).Take(1);

and the exception occures here:

异常发生在这里:

 route.Id =Convert.ToInt32(res.FirstOrDefault());

how can I solve this problem?

我怎么解决这个问题?

采纳答案by Habib

that is because res.FirstOrDefault()returns a RouteLinqstype object, because you are using select p, you may select a field using select p.FieldName, where FieldName is the property you require for conversion, which is probably RouteId

那是因为res.FirstOrDefault()返回一个RouteLinqs类型对象,因为您正在使用select p,您可以选择一个字段 using select p.FieldName,其中 FieldName 是您需要转换的属性,这可能是RouteId

You may want to query the particular field in your linq query.

您可能想要查询 linq 查询中的特定字段。

var res = (from p in aspdb.RouteLinqs
                       orderby p.RouteId descending
                       select p.RouteId).Take(1); //p.RouteID or any field you want. 

Or with your current query, you may do:

或者使用您当前的查询,您可以执行以下操作:

 route.Id =Convert.ToInt32(res.FirstOrDefault().RouteID);

回答by ericosg

Not sure how your is but RouteLinqsis defined, but perhaps you are looking to select a specific object from the class RouteLinqs.

不确定您的RouteLinqs定义是如何定义的,但也许您希望从 class 中选择一个特定的对象RouteLinqs

var res = (from p in aspdb.RouteLinqs
                   orderby p.RouteId descending
                   select p.SomeProperty).Take(1);

In this case pis a single item of RouteLinqsand p.SomeProperty should be your int value.

在这种情况下p是单个项目,RouteLinqsp.SomeProperty 应该是您的 int 值。

回答by Mehmet Ali Sert

This code should work:

此代码应该工作:

route.Id = Convert.ToInt32((res.FirstOrDefault()).FieldName); 

回答by Eren Ers?nmez

Looks like you're selecting the max route id, so why not just do:

看起来您正在选择最大路线 ID,那么为什么不这样做:

route.Id = aspdb.RouteLinqs.Max(x => x.RouteId);

回答by Newred

First make linq :

首先制作 linq :

IQueryable<int> query = (from p in aspdb.RouteLinqs
                           orderby p.RouteId descending
                           select p.Id);

Than run:

比运行:

 int result = query.FirstOrDefault();

In 1 command:

在 1 个命令中:

int result = (from p in aspdb.RouteLinqs
                           orderby p.RouteId descending
                           select p.Id).FirstOrDefault();