473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Oct 26 '08 #1
5 17410
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
Oct 26 '08 #2
On Sat, 25 Oct 2008 20:33:38 -0700, Michael B. Trausch <mi**@trausch.us>
wrote:
>int[] reviews2 = reviews.Cast<int>().ToArray();

What does that do, precisely? I didn't know Nullable<Thad a generic
Cast method. Is that some sort of extension? I don't see it in MSDN.
It's part of the Enumerable class from LINQ:

http://msdn.microsoft.com/en-us/library/bb341406.aspx
Oct 26 '08 #3
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
Oct 26 '08 #4
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
Oct 26 '08 #5
Michael B. Trausch wrote:
On Sat, 25 Oct 2008 22:40:17 -0400
Arne Vajhøj <ar**@vajhoej.dkwrote:
>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<Thad 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
Oct 26 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vijai Kalyan | last post by:
I wrote the following function as a curiosity: template<typename SourceType, typename DestinationType> DestinationType NumericCast(const SourceType& value) { std::wstringstream strbuf; strbuf...
22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Learner | last post by:
Hello, I am trying to store the data entered in a webform in the database. I have few Int and one SmallDateTime filed in my table in SQL Server 2005 database. I have made a storedproc to store the...
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
11
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of...
19
by: tshad | last post by:
I have a value in my sql table set to tinyint (can't set to bit). I am trying to move it into a boolean field in my program and have tried: isTrue = (int)dbReader and isTrue =...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.