C#中的LINQ to SQL查询问题 [英] Problem with LINQ to SQL query in C#

查看:53
本文介绍了C#中的LINQ to SQL查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我在C#中使用LINQ to SQL。

有一个名为db的上下文已经结束三个表hvl85,hvl86,hvl87。

在所有三个表中都有类似的字段和各种记录。

我希望每个表的所有记录都显示在DATAGRIDVIEW基于所选数字,只要用户选择其中一个数字:85,86或87.

我决定只写一个查询。

我写了这些代码如下所示,但我遇到了错误。

请帮助我。谢谢。



我尝试了什么:



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;

推荐答案

你没告诉我们错误是什么,但最有可能的是这是一个汇编错误,表示变量尚未初始化:

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;

编译器可以看到通过该代码的路由,其中​​没有赋值给 mytable :特别是如果数字不是85,86或87.

添加默认你的交换机处理那种可能性的情况,并设置一个初始值。

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.


Quote:

无法将类型'System.Data.Linq.Table< windowsapp.hvl85>'隐式转换为'System.Data.Linq.Table< system.data.linq.itable>'

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



表< T> 类似乎不是接口或委托类型,因此它不能是 covariant [ ^ ]。即使 hvl85 实现 ITable ,您也无法分配表< hvl85> ; 实例到表< ITable> 变量。



相反,你'我需要使用 System.Collections.IEnumerable 界面 [ ^ ]存储查询:


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;


这篇关于C#中的LINQ to SQL查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆