PC Review


Reply
Thread Tools Rate Thread

Convert type 'int?[]' to 'int[]'

 
 
Stuart Shay
Guest
Posts: n/a
 
      26th Oct 2008
Hello all

I have the following LINQ Query

int?[] reviews = (from Review review in bk.Reviews
where (review.Book == bk.ID)
select review.Rating).ToArray();


I need to bind the results to a control that only excepts int[]

If the array in not empty how do I Cast the int?[] array to int[]

Thanks
Stuart

 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      26th Oct 2008
Stuart Shay wrote:
> I have the following LINQ Query
>
> int?[] reviews = (from Review review in bk.Reviews
> where (review.Book == bk.ID)
> select review.Rating).ToArray();
>
>
> I need to bind the results to a control that only excepts int[]
> If the array in not empty how do I Cast the int?[] array to int[]


There must be several ways.

First that came to my mind was:

int[] reviews2 = reviews.Cast<int>().ToArray();

Arne
 
Reply With Quote
 
Michael B. Trausch
Guest
Posts: n/a
 
      26th Oct 2008
On Sat, 25 Oct 2008 21:22:52 -0400
"Stuart Shay" <(E-Mail Removed)> wrote:

> I need to bind the results to a control that only excepts int[]
>
> If the array in not empty how do I Cast the int?[] array to int[]


Your best bet is to use the ?? operator, probably, and foreach on the
int?[] to convert it to an int[]. A quick example:

===== BEGIN C# CODE =====
using System;

public class EntryPoint {
public static void Main() {
int?[] nullable = {0, null, 24, 42, 64, null, 7};
int[] nonnullable = new int[nullable.Length];
int counter = 0;

// This is the important part, and we see below how this works
foreach(int? val in nullable) {
nonnullable[counter++] = val ?? 0;
}

Console.Write("nullable: ");
foreach(int? i in nullable) {
if(i == null) {
Console.Write("(null), ");
} else {
Console.Write("{0}, ", i);
}
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();

Console.Write("nonnullable: ");
foreach(int i in nonnullable) {
Console.Write("{0}, ", i);
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();
}
}
===== END C# CODE =====

You can compile this and you'll get a list of numbers, one with (null)
values in them and the other with 0s instead of (null) values.

You can certainly write a simple method whose sole purpose in life is
to convert int?[] to int[]. The following I've tested as shown, and it
works. It ought to work with any other such type:

===== BEGIN C# CODE =====
using System;

public class EntryPoint {
public static void Main() {
int?[] nullable = {0, null, 24, 42, 64, null, 7};
int[] nonnullable = NullableToRegular<int>(nullable);
int counter = 0;

foreach(int? val in nullable) {
nonnullable[counter++] = val ?? 0;
}

Console.Write("nullable: ");
foreach(int? i in nullable) {
if(i == null) {
Console.Write("(null), ");
} else {
Console.Write("{0}, ", i);
}
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();

Console.Write("nonnullable: ");
foreach(int i in nonnullable) {
Console.Write("{0}, ", i);
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();
}

public static G[] NullableToRegular<G>(Nullable<G>[] items) where G :
struct {
G[] retval = new G[items.Length];
int counter = 0;

foreach(Nullable<G> item in items) {
retval[counter++] = item ?? default(G);
}

return(retval);
}
}
===== END C# CODE =====

There may be a .NET built-in method somewhere that does something like
this, I am not certain. But if not, this one should be pretty close to
what you're looking for. It only took me a few minutes to come up with
once I learned how to write a generic method. Incidentally, I used
"CLR via C#", 2nd ed., Microsoft Press to come up with the information
to write that generic method... good book, great reference.

HTH,
Mike

--
My sigfile ran away and is on hiatus.
http://www.trausch.us/

 
Reply With Quote
 
Michael B. Trausch
Guest
Posts: n/a
 
      26th Oct 2008
On Sat, 25 Oct 2008 22:40:17 -0400
Arne Vajhøj <(E-Mail Removed)> wrote:

> Stuart Shay wrote:
> > I need to bind the results to a control that only excepts int[]
> > If the array in not empty how do I Cast the int?[] array to int[]

>
> There must be several ways.
>
> First that came to my mind was:
>
> int[] reviews2 = reviews.Cast<int>().ToArray();
>


What does that do, precisely? I didn't know Nullable<T> had a generic
Cast method. Is that some sort of extension? I don't see it in MSDN.

--- Mike

--
My sigfile ran away and is on hiatus.
http://www.trausch.us/

 
Reply With Quote
 
Michael B. Trausch
Guest
Posts: n/a
 
      26th Oct 2008
On Sat, 25 Oct 2008 23:26:40 -0400
"Michael B. Trausch" <(E-Mail Removed)> wrote:

> int counter = 0;
>
> foreach(int? val in nullable) {
> nonnullable[counter++] = val ?? 0;
> }


In the second listing, that part needs to be removed to prove what I
stated to be correct. For whatever reason, I did an Undo or something
in my editor before I copied and pasted and it came back. Oops.

--- Mike

--
My sigfile ran away and is on hiatus.
http://www.trausch.us/

 
Reply With Quote
 
Göran Andersson
Guest
Posts: n/a
 
      26th Oct 2008
Stuart Shay wrote:
> Hello all
> I have the following LINQ Query
>
> int?[] reviews = (from Review review in bk.Reviews
> where (review.Book == bk.ID)
> select review.Rating).ToArray();
>
>
> I need to bind the results to a control that only excepts int[]
> If the array in not empty how do I Cast the int?[] array to int[]
>
> Thanks
> Stuart
>


There is no way to cast an int? array to an int array (not even using
unsafe code), as an int? is not the same size as an int.

You have to create a new array and copy each value to it. There are of
course many different ways of doing that, but there is no way around it.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Bjørn Brox
Guest
Posts: n/a
 
      26th Oct 2008
Göran Andersson skrev:
> Stuart Shay wrote:
>> Hello all
>> I have the following LINQ Query
>>
>> int?[] reviews = (from Review review in bk.Reviews
>> where (review.Book == bk.ID)
>> select review.Rating).ToArray();
>>
>>
>> I need to bind the results to a control that only excepts int[]
>> If the array in not empty how do I Cast the int?[] array to int[]
>>
>> Thanks
>> Stuart
>>

>
> There is no way to cast an int? array to an int array (not even using
> unsafe code), as an int? is not the same size as an int.
>
> You have to create a new array and copy each value to it. There are of
> course many different ways of doing that, but there is no way around it.
>

Secondly you have to make a decision on what value to use in the int
array, or if you want to leave them out when you find (int?[n]
== null) (!int?[n].HasValue)
--
Bjørn Brox
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      26th Oct 2008
Michael B. Trausch wrote:
> On Sat, 25 Oct 2008 22:40:17 -0400
> Arne Vajhøj <(E-Mail Removed)> wrote:
>> Stuart Shay wrote:
>>> I need to bind the results to a control that only excepts int[]
>>> If the array in not empty how do I Cast the int?[] array to int[]

>> There must be several ways.
>>
>> First that came to my mind was:
>>
>> int[] reviews2 = reviews.Cast<int>().ToArray();

>
> What does that do, precisely? I didn't know Nullable<T> had a generic
> Cast method. Is that some sort of extension? I don't see it in MSDN.


Yes - it is an extension method.

But since you use LINQ then you must be on .NET 3.5 !

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Import Convert Data Type from Type 1 to Type 2 u473 Microsoft Excel Programming 3 21st Oct 2008 08:22 PM
How can I convert System.Type variables to some database data type? lichaoir Microsoft C# .NET 0 12th Mar 2008 03:35 AM
Is there a way to convert a string representation of a type name into a Type object? Deckarep Microsoft C# .NET 6 18th Jun 2007 03:55 AM
error C2665 :none of the number1 overloads can convert parameter number2 from type 'type' Joseph Lu Microsoft VC .NET 2 17th Jul 2006 01:35 PM
Convert.ChangeType with reference type ie Int32& - can I get the base type? phancey@2bytes.co.uk Microsoft Dot NET 1 8th Feb 2005 03:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:21 AM.