Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否将此变量转换为long列表?_C#_Winforms_Linq To Sql_.net 4.0 - Fatal编程技术网

C# 是否将此变量转换为long列表?

C# 是否将此变量转换为long列表?,c#,winforms,linq-to-sql,.net-4.0,C#,Winforms,Linq To Sql,.net 4.0,我在这里附加了一个屏幕,我需要知道如何将列表转换为长列表 var x = SchedulerMatrixStorage.Resources.Items.Select(col => col.Id); 其中,Id的类型为Object 您可以在Select语句中使用,因为从您的屏幕截图来看,Items集合中的值可能是字符串: var x = SchedulerMatrixStorage.Resources.Items.Select(col => long.Parse(col.Id

我在这里附加了一个屏幕,我需要知道如何将列表转换为长列表

var x = SchedulerMatrixStorage.Resources.Items.Select(col => col.Id);
其中,Id的类型为
Object

您可以在
Select
语句中使用,因为从您的屏幕截图来看,
Items
集合中的值可能是字符串:

var x = SchedulerMatrixStorage.Resources.Items.Select(col => 
   long.Parse(col.Id.ToString())).ToList();
x
将解析为键入
System.Collections.Generic.List

您可以在
Select
语句中使用,因为从您的屏幕截图来看,
项集合中的值可能是字符串:

var x = SchedulerMatrixStorage.Resources.Items.Select(col => 
   long.Parse(col.Id.ToString())).ToList();

x
将被解析为键入
System.Collections.Generic.List

认为这应该满足您的需要

var x = SchedulerMatrixStorage.Resources.Items.Select(col => col.Id).Cast<long>().ToList();
var x=SchedulerMatrixStorage.Resources.Items.Select(col=>col.Id.Cast().ToList();

你认为这应该是你想要的吗

var x = SchedulerMatrixStorage.Resources.Items.Select(col => col.Id).Cast<long>().ToList();
var x=SchedulerMatrixStorage.Resources.Items.Select(col=>col.Id.Cast().ToList();

最快的方法可能是:

var x = SchedulerMatrixStorage.Resources.Items.Select(col => col.Id).Cast<long>();
var x=SchedulerMatrixStorage.Resources.Items.Select(col=>col.Id.Cast();

尽管您确实应该确保您只拥有在作为对象装箱之前很久的Id。

最快的方法可能是:

var x = SchedulerMatrixStorage.Resources.Items.Select(col => col.Id).Cast<long>();
var x=SchedulerMatrixStorage.Resources.Items.Select(col=>col.Id.Cast();

尽管您确实应该确保在将Id作为对象装箱之前很久才获得Id。

如果
Id
类型为
object
,但值始终是字符串,您可以使用:

var x = SchedulerMatrixStorage.Resources
                              .Items
                              .Select(col => long.Parse((string)col.Id)
                              .ToList();
但是,不清楚结果是否总是字符串。您需要向我们提供有关
col.Id
的更多信息

一般来说,您需要以下内容:

var x = SchedulerMatrixStorage.Resources
                              .Items
                              .Select(ConvertColumnId)
                              .ToList();

private static long ConvertColumnId(object id)
{
    // Do whatever it takes to convert `id` to a long here... for example:
    if (id is long)
    {
        return (long) id;
    }
    if (id is int)
    {
        return (int) id;
    }
    string stringId = id as string;
    if (stringId != null)
    {
        return long.Parse(stringId);
    }
    throw new ArgumentException("Don't know how to convert " + id +
                                " to a long");
}

如果
Id
类型为
object
,但值始终为字符串,则可以使用:

var x = SchedulerMatrixStorage.Resources
                              .Items
                              .Select(col => long.Parse((string)col.Id)
                              .ToList();
但是,不清楚结果是否总是字符串。您需要向我们提供有关
col.Id
的更多信息

一般来说,您需要以下内容:

var x = SchedulerMatrixStorage.Resources
                              .Items
                              .Select(ConvertColumnId)
                              .ToList();

private static long ConvertColumnId(object id)
{
    // Do whatever it takes to convert `id` to a long here... for example:
    if (id is long)
    {
        return (long) id;
    }
    if (id is int)
    {
        return (int) id;
    }
    string stringId = id as string;
    if (stringId != null)
    {
        return long.Parse(stringId);
    }
    throw new ArgumentException("Don't know how to convert " + id +
                                " to a long");
}

由于您的列表看起来是同质的,因此您可以利用.NET framework中的内置转换函数:

var x = SchedulerMatrixStorage.Resources.Items
       .Select(col => Convert.ToInt64(col.Id)).ToList();
Convert
静态类提供了几个转换函数,其中一组将把各种类型的对象转换为各种内置值类型,如
long
。大多数函数还有一个重载,它接受一个
对象
参数(我假设您的集合基本上包含这个参数)


请注意,如果提供的对象无法转换为
(例如,如果您要传入
表单
),则此函数将在运行时失败,但从您的屏幕截图判断,这应该适合您。

因为您的列表似乎是同质的,您可以利用作为.NET framework一部分的内置转换函数:

var x = SchedulerMatrixStorage.Resources.Items
       .Select(col => Convert.ToInt64(col.Id)).ToList();
Convert
静态类提供了几个转换函数,其中一组将把各种类型的对象转换为各种内置值类型,如
long
。大多数函数还有一个重载,它接受一个
对象
参数(我假设您的集合基本上包含这个参数)


请注意,如果提供的对象无法转换为
长的
(例如,如果您要传入
表单
),则此函数将在运行时失败,但从您的屏幕截图判断,这应该对您有效。

好吧,我成功地将第一个字符串
“-1”
转换为正确的长字符串。行
varx=SchedulerMatrixStorage.Resources.Items.Select(col=>Convert.ToInt64(col.Id)).ToList()现在完全适合我了。Id将始终是一个长的。有了这条规则,有没有最好的方法来实现这一点?我的意思是,我的任务已经完成了,但我现在想知道,如果LINQ提供了一个更好的方法


嗯,我成功地将第一个字符串
“-1”
转换为一个适当的长字符串。行
varx=SchedulerMatrixStorage.Resources.Items.Select(col=>Convert.ToInt64(col.Id)).ToList()现在完全适合我了。Id将始终是一个长的。有了这条规则,有没有最好的方法来实现这一点?我的意思是,我的任务已经完成了,但我现在想知道,如果LINQ提供了一个更好的方法


为什么
解析
?我认为它是对象,而不是字符串。
Parse
Cast
有什么好处?如果所有对象都是
long
类型?@gaearon他的第一项是:
“-1”
,这是一个字符串。对于他的数据,这将失败,因为只有第一项(在他显示的列表中)是字符串。“其他演员都会失败。”亚当说得很好。编辑为使用
ToString()
而不是强制转换;但是,我更喜欢使用
Convert.ToInt64
。为什么
Parse
?我认为它是对象,而不是字符串。
Parse
Cast
有什么好处?如果所有对象都是
long
类型?@gaearon他的第一项是:
“-1”
,这是一个字符串。对于他的数据,这将失败,因为只有第一项(在他显示的列表中)是字符串。“其他演员都会失败。”亚当说得很好。编辑为使用
ToString()
而不是强制转换;但是,我更喜欢您使用
Convert.ToInt64
。这会因为他提供的值而失败,因为第一个值是字符串。这会因为他提供的值而失败,因为第一个值是字符串。与类似的答案一样,这对他来说也会失败。他的第一项是字符串。和类似的答案一样,这对他来说是失败的。他的第一项是字符串。当你发布答案时,你不能问新问题。当你发布答案时,没有人会收到通知,所以没有人会看到这一点。问一个新问题,或者在某人的回答中添加一条评论,提到他的名字,比如:@Hassan。当你发帖时,你不能问一个新问题