开发者

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?

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

Where, Id is of type 开发者_运维百科Object

Convert this var to a list of longs?


Since your list appears to be homogenous, you can take advantage of the built-in conversion functions that are part of the .NET framework:

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. Most of these functions also have an overload that takes an object parameter (which I'm assuming is what your collection fundamentally contains).

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.


If Id is of type object but the value is always a string, you could use:

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.

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


You can use long.Parse() in your Select statement, since from your screenshot it looks like values in the Items collection could be strings:

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

x will be resolved to type System.Collections.Generic.List<long>.


Think this should do what you want

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.


Well, I managed to convert the first string "-1" to a proper long. The line var x = SchedulerMatrixStorage.Resources.Items.Select(col => Convert.ToInt64(col.Id)).ToList(); work perfectly for me now. The Id will always be a long. 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...

Convert this var to a list of longs?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜