سؤال

I have a linq query which is giving me desired output :

 var data = (from c in dtskip.AsEnumerable()
                        select new[]  { 
                         c.Field<string>("Suburb"), c.Field<string>("Postcode"), c.Field<string>("State"),c.Field<string>("ID"), c.Field<string>("SEARCHKEY"), c.Field<string>("RATING"), c.Field<string>("DELIVERY")
                       });

How can i select all the column instead of giving name like c.field<string>("postcode") .My output is the data only from datatable dtskip :

output:

["DARWIN","0800","NT","2","DARWINNT","A","Delivery Area"]
,["ALAWA","0810","NT","5","ALAWANT","A","Delivery Area"],
["BRINKIN","0810","NT","6","BRINKINNT","A","Delivery Area"],

is there any other way i can get the output in dis way from datatable using linq query .

هل كانت مفيدة؟

المحلول

DataRow contains an ItemArray member which returns all the data in that row as an array, the downside is they are all returned as objects but if all your columns are the same type you can cast the ItemArray in line to the desired type (in this case string)

dtskip.Rows.Cast<DataRow>().Select(r => r.ItemArray.Cast<string>());

This will give you an IEnumerable<IEnumerable<string>> to work with.

نصائح أخرى

have you tried

var data = (From c in dtskip
            select c).AsEnumerable(); //Not sure about the AsEnumerable :s

Are you looking for something like this?

var data = dtskip.AsEnumerable().
            Select(x => new 
                   { 
                              Suburb = x.Field<string>("Suburb"),
                              Postcode= x.Field<string>("Postcode"),
                              State= x.Field<string>("State"),
                              Id= x.Field<string>("ID"),
                              Searchkey = x.Field<string>("SEARCHKEY"),
                              Rating = x.Field<string>("RATING"),
                              Delivery = x.Field<string>("DELIVERY")
                   });
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top