0

如何在 Linq to SQL 中使用字符串字段进行动态比较以获得如下查询:

"SELECT COLUMN_1, COLUMN_2" +
"FROM "+ stringTABLE +
"WHERE " + stringCOLUMN_1_or2  + " = ' " + 8 + "'";

我知道我可以这样做:

from x in objects
where x.COLUMN_1 == "8"
select x

但 FIELD 是可变的,可以是 COLUMN_1 或 COLUMN_2" 或其他...

作为我想在 Windows 窗体(框架 4.0)中执行的操作的示例

from x in objets
where x.field[stringCOLUMN_1_or2] == "8"
select x

我怎么能这样做?提前致谢

4

5 回答 5

3

通常,LINQ to SQL 将创建一个与您的列名称相同的属性,因此您的 LINQ 可能如下所示:

from x in objets
where x.stringCOLUMN_1 == 8.ToString()
select x;
于 2013-01-11T19:33:31.183 回答
1

Not sure what objects is, or your field collection, but first you should have quotes around the literal if it is a string comparison:

from x in objets
where x.field[stringCOLUMN_1] == "8"
select x

Usually your column names are represented as properties.

from x in objets
where x.stringCOLUMN_1 == "8"
select x

Lastly, usually you have a DBContext and you need to drill into it to select a specific table:

from x in db.TableNameHere
where x.stringCOLUMN_1 == "8"
select x

Without seeing more context to your code it's hard to provide more suggestions.

If you're looking to do something more dynamic, note you can append additional where criteria using extension methods:

var query = from x in db.TableNameHere select x;
if(ShouldFilterColumn1)
  query = query.Where(x => x.Column1 == "8");

var results = query.ToList();

Without seeing more context to your code it's hard to provide more suggestions.

于 2013-01-11T19:36:39.993 回答
0

我能想到三种可能。

第一个是动态 LINQ。我调查过一次,但没有走得太远。

第二种选择(类似于 AaronLS 的建议)是一次构建查询。这是我最常使用的选项。
注意:您必须在 select 或 order 子句之前添加 where 子句,否则您将在运行时遇到异常。

var query = from x in objects;
switch (columnIndicator)
{
    case ColumnIndicatorTypes.Column1:
        query = query.Where(x => x.Column1 == "8");
        break;
    case ColumnIndicatorTypes.Column2:
        query = query.Where(x => x.Column2 == "8");
        break;
    case ColumnIndicatorTypes.Column3:
        query = query.Where(x => x.Column3 == "8");
        break;
    case ColumnIndicatorTypes.Column4:
        query = query.Where(x => x.Column4 == "8");
        break;
}

第三个选项是对所有选项进行编码。例如:

switch (columnIndicator)
{
    case ColumnIndicatorTypes.Column1:
        query = from x in objects
            where x.COLUMN_1 == "8"
            select x;
        break;
    case ColumnIndicatorTypes.Column2:
        query = from x in objects
            where x.COLUMN_2 == "8"
            select x;
        break;
    case ColumnIndicatorTypes.Column3:
        query = from x in objects
            where x.COLUMN_3 == "8"
            select x;
        break;
    case ColumnIndicatorTypes.Column4:
        query = from x in objects
            where x.COLUMN_4 == "8"
            select x;
        break;
    default:
        query = from x in objects
            select x;
        break;
}

如果您想查看生成的查询,我建议您db.Log在运行查询之前连接(类似于db.Log = New System.IO.StringWriter(New StringBuilder())),在您的服务器上运行探查器(例如 SQL Server Profiler),或者将对象转换为字符串(我认为这会返回字符串版本的查询,但已经有一段时间了)。

于 2013-01-11T20:19:33.943 回答
0

您可以使用ExpressionTree

如何:使用表达式树构建动态查询(C# 和 Visual Basic)

使用 Expression 使用嵌套类创建谓词,其中Marc Gravell 的答案看起来简单且可行。

更新: 对于 LinqToSql,上面的链接没有帮助。

查看 Trisped 答案(第二个选项),它可能会有所帮助。

于 2013-01-11T20:13:38.357 回答
0

我想你可以使用反射:

from x in db.TableName
    where (x.GetType().GetProperty(stringCOLUMN_1_or2).GetValue(x, null)) == " 8"
    select x;

不确定是否有任何简单的 Linqish 方法可以做到这一点......

编辑:假设动态 Linq 可以工作,它只是:

from x in objets
.Where(stringCOLUMN_1_or2  + " = ' " + 8 + "'")
select x

这里有一些关于使用 SQL 的动态 Linq 的更多信息:http ://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-库.aspx

于 2013-01-11T20:16:19.867 回答