带有泛型表的LINQ查询 [英] LINQ query with a generic table

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

问题描述

我试着在我的方法中只有一个选择。该方法将选择一个基于定义字符串的表并执行select。

I'm tryng to have only one select in my method. That method will select a table based in a defined string and execute a select.

private static void Load()
{
    DataClassesDataContext contexto = new DataClassesDataContext();
    IQueryable newTable = null;

    string tabela = "A";

    switch (tabela)
    {

        case "A":
            newTable = contexto.GetTable<Table2>();
            break;
        case "B":
            newTable = contexto.GetTable<Table1>();
            break;
        default:
            break;
    }

    var genericQuery = from a in newTable
                       select a;
}

通过正确的表值可以做些什么?

What can I do to have genericQuery with the right table values?

推荐答案

除非泛化Load方法,否则这将不起作用:

This won't work unless you genericize the Load method:

private static void Load<T>()
{
   ...
   var genericQuery = contexto.GetTable<T>();
   ...
}

这篇关于带有泛型表的LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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