Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:38:53+00:00 2026-06-15T08:38:53+00:00

Having three tables (a simplified approach to what I am trying to do) I

  • 0

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.

enter image description here

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 )

enter image description here

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:38:54+00:00Added an answer on June 15, 2026 at 8:38 am

    If you are happy enough to end up with a datatable, then you can do something like the following.

    var data = (from r in Licenses 
                where r.Category == 1
                select new 
                { 
                     Owner       = r.LicenseOwner.Name , 
                     LicenseType = r.LicenseType.Name,  
                     Status      = r.Status 
                }
                ).ToList();
    

    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

    var lookup = data.ToLookup(r => r.Owner , 
                               r => new   
                                  { 
                                  License = r.LicenseType  , 
                                  Status = r.Status
                                  } 
                               ).OrderBy(r=>r.Key);
    

    Then retrieve the columns names and build the datatable from that.

    var columns = (from r in data 
                        orderby r.LicenseType 
                        select r.LicenseType
                      ).Distinct().ToList();
    
    DataTable dt = new DataTable();
    dt.Columns.Add("Owner");
    foreach(var r in columns)
        dt.Columns.Add(r);
    

    Then populate the datatable from the lookup

    foreach(var r in lookup)
    {
        DataRow dr = dt.NewRow();
        dr["Owner"]  = r.Key ;
    
        foreach(var column in columns)
            dr[column] = "";
    
        foreach(var entry in r)
            dr[entry.License] = entry.Status;
        dt.Rows.Add(dr);
    }
    

    And if you include a reference to System.Data.DataSetExtensions, you can use linq to query the resultant datatable, eg

     var owner1 = (from r in dt.AsEnumerable() 
                      where r.Field<string>("Owner") == "Owner 1"      
                      select r
                  ).SingleOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having three images or buttons in a linear layout which fits fine
I am having three tables which has primary keys and references. I need to
I am having a problem trying to JOIN across a total of three tables:
I have two tables. I am trying to find rows in one table which
I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares. if suppose, Users table having 6 user
i am having three tables namely 1)cd_register |----------------------------------------------------| | username | name | age
I'm having some trouble using constraints correctly. I have three tables, 'item', 'store' and
I have three tables A, B, C. A having Id,Name,Address coulmns. B is the
Given three tables: a car table, an extras table and a link table, something
am having a problem constructing a query here is simplified tables structure 3 tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.