Having three tables (a simplified approach to what I am trying to do) I am trying to construct a table with dynamic cols.
The layout should be a grid showing which licenses each owner has. This is retrieved from a table where I first need to do a distinct select to get the header columns of my table.

Here’s how I retreive the header the table, to get the dynamic columns
var tableHeader = (from l in Licenses
join lt in LicenseTypes on l.LicenseTypeId equals lt.ID
where l.Category == 1
select new
{
l.LicenseTypeId,
lt.Name
}).Distinct().OrderBy (x =>x.Name )

I can’t quite see what’s my best bet to use this query together with my headerquery to compose my table
var tableData = (from l in Licenses select l)
One approach is to iterate the tableData and for each distinct owner, build the data rows and having sub queries retreiving data such as Owners name to present in grid. This however will result in a series of transactions to the database, so I am wondering if this somehow may be done in a more elegant way.
Here’s a script for generating data for my example:
if exists (select * from sysobjects where name = 'LicenseType') drop table LicenseType
if exists (select * from sysobjects where name = 'License') drop table License
if exists (select * from sysobjects where name = 'LicenseOwner') drop table LicenseOwner
go
create table LicenseType
(
ID int not null primary key,
Name nvarchar(30) not null
)
create table LicenseOwner
(
ID int not null primary key,
Name nvarchar(30) not null
)
create table License
(
ID int not null primary key,
LicenseTypeId int null references LicenseType(ID),
LicenseOwnerId int null references LicenseOwner(ID),
Status nvarchar(30),
Category int not null
)
insert LicenseType values (1, 'A001')
insert LicenseType values (2, 'A002')
insert LicenseType values (3, 'A003')
insert LicenseType values (4, 'A004')
insert LicenseType values (5, 'C001')
insert LicenseType values (6, 'X001')
insert LicenseOwner values (1, 'Owner 1')
insert LicenseOwner values (2, 'Owner 2')
insert LicenseOwner values (3, 'Owner 3')
insert LicenseOwner values (4, 'Owner 4')
insert LicenseOwner values (5, 'Owner 5')
insert License values (1, 1, 1, 'OK', 1)
insert License values (2, 2, 1, 'Invalid', 1)
insert License values (3, 3, 1, 'OK', 1)
insert License values (4, 6, 1, 'Pending', 1)
insert License values (5, 1, 1, 'OK', 1)
insert License values (6, 2, 1, 'OK', 1)
insert License values (7, 6, 1, 'Invalid', 1)
If you are happy enough to end up with a datatable, then you can do something like the following.
This should retrieve all your information you need with one database lookup and can be filtered if required (as in the example selecting just category 1)
You can then use ToLookup to group all the (in memory) records by owner, ie
Then retrieve the columns names and build the datatable from that.
Then populate the datatable from the lookup
And if you include a reference to System.Data.DataSetExtensions, you can use linq to query the resultant datatable, eg