如何在LINQ语句中将int转换为double

时间:2016-10-10 12:02:00

标签: c# linq

我有这行代码:

IEnumerable<double> katlar = ilanlarQuery.ToList()
                                         .Select(r => r.BulunduguKat.Value)
                                         .OrderBy(f => f);

我收到错误,因为BulunduguKat.Value是int,但katlar是double。如何在此声明中将BulunduguKat.Value转换为double?感谢。

2 个答案:

答案 0 :(得分:9)

最直接的方式:只需转换为double即可。通过这样做,您告诉TResultSelect操作的double类型是IEnumerable<double> - 使其成为IEnumerable<double> katlar = ilanlarQuery.Select(r => (double)r.BulunduguKat.Value) .OrderBy(f => f);

IEnumerable<double> katlar  = ilanlarQuery.Select<YourType,double>(i => i.BulunduguKat.Value)
                                          .OrderBy(f => f)();

由于int可以隐式转换为double,因此也可以使用:

.ToList()

此外,如您所见,您可以在之前删除BulunduguKat

如果Tim建议确实Nullable<int>//If you don't want them in the result collection: var katlar = ilanlarQuery.Where(r => r.BulunduguKat.HasValue) .Select(r => (double)r.BulunduguKat.Value) .OrderBy(f => f) //If you want them with some default value: var katlar = ilanlarQuery.Select(r => (double)(r.BulunduguKat.HasValue ? r.BulunduguKat.Value : 0)) .OrderBy(f => f); ,那么您应该决定:

Sub LöscheDatensatz()
Application.ScreenUpdating = False
With ActiveSheet
    .unprotect Password:="test"
        If Cells(Selection.Row, 1).Locked = False Then
            Selection.EntireRow.Delete
        End If
    .Protect Password:="test", AllowFiltering:=True
End With
Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

ds.Tables["#KAMPANYA"].AsEnumerable().Select(s => Convert.ToInt32(s["KAMNO"])).ToList()