Click here to Skip to main content
14,031,218 members
Rate this:
 
Please Sign up or sign in to vote.
Hello everyone

I’m using LINQ to SQL in C#.
There is a Context named “db” which is concluded of three tables “hvl85, hvl86, hvl87”.
There are similar fields with various records in all three tables.
I want all records of each table to be shown in DATAGRIDVIEW based on the chosen number whenever the user selects one of these numbers: 85, 86 or 87.
I have decided to write just one Query.
I have written these codes as you see below but I encounter an error.
Please help me. Thank you.

What I have tried:

Table<hvl85> table85 = db.GetTable<hvl85>();
Table<hvl86> table86 = db.GetTable<hvl86>();
Table<hvl87> table87 = db.GetTable<hvl87>();

Table<itable> mytable;

switch (number)
{
    case 85:
        mytable = table85;
        break;
    case 86:
        mytable = table86;
        break;
    case 87:
        mytable = table86;
        break;
}
var queryAllRecords = from q in mytable select q;
dataGridView1.DataSource = queryAllRecords;
Posted
Updated 26-Feb-19 8:05am
v2
Comments
Patrice T 22-Feb-19 15:54pm
   
'but I encounter an error.'
Which one ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You don't tell us what the error is, but the most likely is that it's a compilation error saying that a variable has not been initialized:
Table<itable> mytable;

switch (number)
{
case 85:
mytable = table85;
break;
case 86:
mytable = table86;
break;
case 87:
mytable = table86;
break;
}
var queryAllRecords = from q in mytable select q;
The compiler can see a route through that code where no value is assigned to mytable : specifically if number is not 85, 86, or 87.
Add a default case to your switch which handles that eventuality, and set an initial value.
   
Comments
Pouria Polouk 23-Feb-19 0:20am
   
Thank you for your attention.
Despite writing the Default part, there are three errors related to the tables:

Cannot implicitly convert type 'System.Data.Linq.Table<windowsapp.hvl85>' to 'System.Data.Linq.Table<system.data.linq.itable>'
Cannot implicitly convert type 'System.Data.Linq.Table<windowsapp.hvl86>' to 'System.Data.Linq.Table<system.data.linq.itable>'
Cannot implicitly convert type 'System.Data.Linq.Table<windowsapp.hvl87>' to 'System.Data.Linq.Table<system.data.linq.itable>'
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Quote:
Cannot implicitly convert type 'System.Data.Linq.Table<windowsapp.hvl85>' to 'System.Data.Linq.Table<system.data.linq.itable>'

The Table<T> class doesn't seem to be an interface or delegate type, so it cannot be covariant[^] on its generic type parameter. Even if hvl85 implements ITable, you won't be able to assign a Table<hvl85> instance to a Table<ITable> variable.

Instead, you'll need to use the System.Collections.IEnumerable interface[^] to store the query:
System.Collections.IEnumerable queryAllRecords;
switch (number)
{
    case 85:
    {
        queryAllRecords = db.GetTable<hvl85>();
        break;
    }
    case 86:
    {
        queryAllRecords = db.GetTable<hvl86>();
        break;
    }
    case 87:
    {
        queryAllRecords = db.GetTable<hvl87>();
        break;
    }
    default:
    {
        queryAllRecords = null;
        break;
    }
}

dataGridView1.DataSource = queryAllRecords;
   
Comments
Pouria Polouk 28-Feb-19 14:24pm
   
thank you.
But how can I select some fields from the table with method?
Richard Deeming 28-Feb-19 14:47pm
   
queryAllRecords = db.GetTable<hvl87>l().Select(row => new { ... });

Or:
queryAllRecords = from q in db.GetTable<hvl87>() select new { ... };
Pouria Polouk 4-Mar-19 9:15am
   
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190419.4 | Last Updated 26 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100