views:

55

answers:

3

This is not working. Returns Null to dept_list.

        var dept_list = ((from map in DtMapGuestDepartment.AsEnumerable()
                         where map.Field<Nullable<long>>("Guest_Id") == 174
                         select map.Field<Nullable<long>>("Department_id")).Distinct())as IEnumerable<DataRow>;
       DataTable dt = dept_list.CopyToDataTable(); //dept_list comes null here

This works as desired.

        var dept_list = from map in DtMapGuestDepartment.AsEnumerable()
                         where map.Field<Nullable<long>>("Guest_Id") == 174
                         select map;
       DataTable dt = dept_list.CopyToDataTable(); //when used like this runs correct.

What mistake is being done by me here. ?

+3  A: 

Your first query is returning an enumerable of values (of the department IDs) instead of an enumerable of data rows (as in the second query).

Since IEnumerable<Nullable<long>> is not a subtype of IEnumerable<DataRow>, the as operator returns null.

(As a side note, using a normal cast instead of as would have given you an InvalidCastException, which is more helpful in finding errors than just returning null.)


EDIT: If you really need a DataTable in the end, I guess you will have to construct it manually (untested):

var dept_list = ((from map in DtMapGuestDepartment.AsEnumerable()
                     where map.Field<Nullable<long>>("Guest_Id") == 174
                     select map.Field<Nullable<long>>("Department_id")).Distinct())

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Department_id", typeof(long?)));
foreach (long? dept in dept_list) {
    dt.Rows.Add(dept);
}
Heinzi
@Heinzi: What I need to do so as to correct it. I changed Nullable<long> to DataRow and it gives me error on debugging. Unable to cast object of type 'System.Int64' to type 'System.Data.DataRow'.
Shantanu Gupta
@Shantanu: Yes, that's because the department ID *is* a Nullable<long> and not a DataRow. As far as I know, you cannot use LINQ to remove columns from an IEnumerable<DataRow> (and still return an IEnumerable<DataRow>). If possible, use the first variant but don't convert it to a DataTable; just use the IEnumerable<Nullable<long>>.
Heinzi
@Heinzi: Could u suggest me some way so as to get this record in form of column rather than long value. I have been fighting with many of these things and not being able to make any picture of how all these things are going on. How data is being processed. In my scenario in first case. How will be able to copy records in DataTable if i removes IEnumerable<DataRow>
Shantanu Gupta
@Shantanu: Why do you need a DataTable at all? Can't you just use the IEnumerable<Nullable<long>> directly?
Heinzi
@Heinzi: I am creating a user control for window application where i will be passing this datatable. That control will query again using this datatable and other datatable to get some records and populate it on my user control. This will be having 1 to many relationship. Please let me know another solution if you have some
Shantanu Gupta
@Shantanu: I've edited my answer to show how to create a new DataTable.
Heinzi
@Heinzi: I appreciate for your support. And would like to suggest one more way, plz let me know which one will be fast. Get data into datatable using second method that was executing successfully and then use dt.columns.remove() to remove non desired columns. But will it give me distinct records ?
Shantanu Gupta
@Heinzi: Could you please tell me what is the purpose of (long? dept in dept_list) I dont know what it is called and what is its functionality
Shantanu Gupta
`long?` is just another way of writing `Nullable<long>`. The rest is a regular foreach statement: http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx
Heinzi
@Shantanu: No, the second method will not give you distinct records, but you can use `distinct` after copying the table and removing the columns, see here: http://social.msdn.microsoft.com/Forums/en/adodotnetdataset/thread/ed9c6a6a-a93e-4bf5-a892-d8471b84aa3b
Heinzi
+1  A: 

It's the cast as IEnumerable<DataRow> that is probably failing. If T is not convertible to U, then the expression foo as U will return null for any T foo. It looks like the result of the first LINQ statement (up to the as expression) is actually an IEnumerable<long?>.

The second statement works since you're letting type inference do the work for you.

Chris Schmich
A: 

in the first example you select map.Field>("Department_id")) so its return value is not IEnumerable

Itay