Specifying Returned Rows in LINQ2DataSet

I have a requirement to retrieve a separate subset of rows from a DataTable and thought LINQ2DataSets might be a useful and clean way to do this, however it seems that it is not possible to simply determine the return rows from a LINQ2DS query as follows

var result = from r in fips.AsEnumerable() select
r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
r.Field<string>("PROCESS_SUB_GROUP_NAME"),
r.Field<string>("...

      

when i start getting errors after the first comma.

Is this a correct guess, and how could I work around it to return a subset of the columns from the dataset in order to apply the Distinct () method to?

+1


source to share


1 answer


You forgot the new operator and field names:

var result = from r 
    in fips.AsEnumerable() 
    select new
    {
        FacProcess = r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
        GroupName = r.Field<string>("PROCESS_SUB_GROUP_NAME"),
        Item3 = r.Field<string>("Item3")
    };

      

You can also explicitly declare that you are going to use the type:

var result = from r 
    in fips.AsEnumerable() 
    select new MyType("InitClassParams") 
    {
        FacProcess = r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
        GroupName = r.Field<string>("PROCESS_SUB_GROUP_NAME"),
        Item3 = r.Field<string>("Item3")
    };

      

Scott Guthrie (VP Developer Devision, Microsoft) has some good information about LINQ (he talks about LINQ to SQL, but most of them apply independently).



Then apply a separate sentence:

var result = from r 
    in fips.AsEnumerable() 
    select new
    {
        FacProcess = r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
        GroupName = r.Field<string>("PROCESS_SUB_GROUP_NAME"),
        Item3 = r.Field<string>("Item3")
    }
    distinct;

      

Then put it in the list or navigate to it. Nothing will be selected / allocated / etc until something like the following is executed:

var list = result.ToList()
foreach(var item in result) {}

      

+4


source







All Articles