简体   繁体   中英

How to convert a var which contains Long to an Long[]

I have the following:

var resourceids = racks.Select(a => a.Technology.IT360ID.Value);
var resource = repository.GetResources(resourceids);

But i am not sure how i can convert the var resourceids to long[] as my repository method accept long[]

Assuming a.Technology.IT360ID.Value is a long then resourceids is actually an IEnumerable<long> . So you just need to convert it using ToArray() :

var resourceids = racks.Select(a => a.Technology.IT360ID.Value).ToArray();
var resource = repository.GetResources(resourceids);

Remember that var is not a "type" - it just lets the compiler determine the type versus declaring it explicitly.

This may be a case where it's better to explicitly declare the type versus using var :

long[] resourceids = racks.Select(a => a.Technology.IT360ID.Value).ToArray();
var resource = repository.GetResources(resourceids);

The method Select will return IEnumerable<long> . So, to convert to long[] , you can call ToArray() which forces immediate query evaluation and returns an array that contains the query results:

var resourceids = racks.Select(a => a.Technology.IT360ID.Value)
                       .ToArray();

var resource = repository.GetResources(resourceids);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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