简体   繁体   English

将此var转换为long列表?

[英]Convert this var to a list of longs?

I have a screen attached here and I need to know how I can convert the list to a list of Longs? 我的屏幕附在此处,我需要知道如何将列表转换为Longs列表?

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

Where, Id is of type Object 其中,Id是Object类型

在此处输入图片说明

Since your list appears to be homogenous, you can take advantage of the built-in conversion functions that are part of the .NET framework: 由于您的列表看起来是同质的,因此您可以利用.NET框架中的内置转换功能:

var x = SchedulerMatrixStorage.Resources.Items
       .Select(col => Convert.ToInt64(col.Id)).ToList();

The Convert static class provides several conversion functions, one group of which will convert objects of various types to various built-in value types, like long . Convert静态类提供了几种转换函数,其中一组将将各种类型的对象转换为各种内置值类型,例如long Most of these functions also have an overload that takes an object parameter (which I'm assuming is what your collection fundamentally contains). 这些函数中的大多数还具有带有object参数的重载(我假设这是您的集合从根本上包含的内容)。

Note that this function will fail at runtime if the supplied object is something that can't be converted into a long (such as if you were to pass in, say, a Form ), but judging by your screenshot this should work for you. 请注意,如果提供的对象不能被转换为long (例如,如果要传递,例如Form ),则此函数将在运行时失败,但是从屏幕截图来看,此方法对您来说很有效。

You can use long.Parse() in your Select statement, since from your screenshot it looks like values in the Items collection could be strings: 您可以在Select语句中使用long.Parse() ,因为从屏幕截图long.Parse()Items集合中的值可能是字符串:

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

x will be resolved to type System.Collections.Generic.List<long> . x将解析为System.Collections.Generic.List<long>类型。

If Id is of type object but the value is always a string, you could use: 如果Idobject类型,但值始终是字符串,则可以使用:

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

However, it's not clear that the result always is a string. 但是,不清楚结果是否始终字符串。 You need to give us more information about col.Id . 您需要向我们提供有关col.Id更多信息。

In general, you want something like this: 通常,您需要这样的东西:

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");
}

认为这应该做你想要的

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

Probably the quickest way is this: 最快的方法可能是这样的:

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

although you should really make sure you've only got Id's that are long before they were boxed as an object. 尽管您应该真正确保仅获得将ID装箱为对象之前很久的ID。

Well, I managed to convert the first string "-1" to a proper long. 好吧,我设法将第一个字符串"-1"转换为适当的长度。 The line var x = SchedulerMatrixStorage.Resources.Items.Select(col => Convert.ToInt64(col.Id)).ToList(); var x = SchedulerMatrixStorage.Resources.Items.Select(col => Convert.ToInt64(col.Id)).ToList(); work perfectly for me now. 现在非常适合我。 The Id will always be a long. Id永远很长。 With that rule assured, is there a best method to accomplish this? 在确保该规则的前提下,是否有最佳方法来实现这一目标? I mean, my task is done but wanted to now if LINQ offer an even better method... 我的意思是,我的任务已经完成,但现在想让LINQ提供更好的方法...

在此处输入图片说明

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

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