D
David
How do I specify what columns to include in a Linq to Dataset query?
I have a dataset with four rows: A key (int) field, a Customer(string)
field, a Facility(string) field, and a station(string) field. Basically,
this dataset contains the results of a query of stations. A customer can
have several facilities. A facility can have several stations. The
combination of customer, facility, and station uniquely identifies a station.
What I want is, for a given customer, query the table and get a list of
facilities. My first try was:
IEnumerable<DataRow> facilitiesquery = (from r in
dsPlantInventory.Tables[0].AsEnumerable()
where
r.Field<string>("Customer").Equals(custname)
select
r).Distinct<DataRow>(System.Data.DataRowComparer.Default);
This almost worked, but of course, the Distinct clause returned distinct
rows, not distinct facility entries. What I really want is to a query that
returns a set of DataRow objects. Each datarow would have one column, which
is "facility" and all duplicates would be removed.
In SQL, it would look like: "Select Distinct Facility from StationTable
Where Customer='custname' //replace 'custname' with the actual customer
name that was passed in.
I tried to replace the final r with r.Field<string>("Facility") , but of
course I ended up with a list of strings, not a list of DataRows, and my
Distinct clause failed.
My Linq book unfortunately has no examples where the Linq query in that form
returns a set of data rows that contain only a subset of fields from the
original table.
I have a dataset with four rows: A key (int) field, a Customer(string)
field, a Facility(string) field, and a station(string) field. Basically,
this dataset contains the results of a query of stations. A customer can
have several facilities. A facility can have several stations. The
combination of customer, facility, and station uniquely identifies a station.
What I want is, for a given customer, query the table and get a list of
facilities. My first try was:
IEnumerable<DataRow> facilitiesquery = (from r in
dsPlantInventory.Tables[0].AsEnumerable()
where
r.Field<string>("Customer").Equals(custname)
select
r).Distinct<DataRow>(System.Data.DataRowComparer.Default);
This almost worked, but of course, the Distinct clause returned distinct
rows, not distinct facility entries. What I really want is to a query that
returns a set of DataRow objects. Each datarow would have one column, which
is "facility" and all duplicates would be removed.
In SQL, it would look like: "Select Distinct Facility from StationTable
Where Customer='custname' //replace 'custname' with the actual customer
name that was passed in.
I tried to replace the final r with r.Field<string>("Facility") , but of
course I ended up with a list of strings, not a list of DataRows, and my
Distinct clause failed.
My Linq book unfortunately has no examples where the Linq query in that form
returns a set of data rows that contain only a subset of fields from the
original table.