简体   繁体   English

如何使用LINQ和变量动态查询数据库?

[英]How to dynamically query the database using LINQ and a variable?

I have a database table that has 100 columns, labeled D1,D2,D3 and so on out to 100.我有一个数据库表,它有 100 列,标记为 D1、D2、D3,依此类推到 100。

In my method (ASP.NET MVC) I need to check if these columns are null, before displaying them in a table in my view.在我的方法 (ASP.NET MVC) 中,我需要检查这些列是否为空,然后再将它们显示在我视图中的表中。 I really don't want to have to do this :我真的不想这样做

if(db.Datatable.Select(x=>x.D1).FirstOrDefault() != null)
{
  //do code
}
if(db.Datatable.Select(x=>x.D2).FirstOrDefault() != null)
{
  //do code
}
if(db.Datatable.Select(x=>x.D3).FirstOrDefault() != null)
{
  //do code
}

That would be 100 if statements and just look horrible.这将是 100 个 if 语句,看起来很糟糕。 So I started this code here:所以我在这里开始了这段代码:

  for(int i = 1; i < 100; i++)
  {
       var dbField = "D" + i;  //D1,D2,D3....

       var currField = db.Datatable.Select(x => x.dbField).FirstOrDefault();


       //here i would add the value (0 for null, 1 for data) to an array, which i would handle in the view
  }

Except LINQ/C# does not like this.除了 LINQ/C# 不喜欢这个。 It obviously throws an error on dbField in the Select statement.它显然在 Select 语句中的 dbField 上引发错误。 You can't just set .Select(x=x.variable).你不能只设置 .Select(x=x.variable)。 I have read on creating your own lambda expression tree and building a custom dynamic query, any thoughts on how to do this?我已阅读有关创建您自己的 lambda 表达式树并构建自定义动态查询的内容,对如何执行此操作有任何想法吗? How can I custom create the field name for the Select statement?如何为 Select 语句自定义创建字段名称? Thank you.谢谢你。

There is no reason to repeatedly take the first element from the same unchanging collection.没有理由重复从同一个不变的集合中取出第一个元素。 Simple take the first element from the collection, and then work with that retrieved object:简单地从集合中取出第一个元素,然后使用检索到的对象:

var element = db.Datatable.FirstOrDefault();

This already dramatically simplifies your code and improves performance (because you don't need to iterate over the collection for every property):这已经极大地简化了您的代码提高了性能(因为您不需要迭代每个属性的集合):

if(element.D1 != null)
{
    //do code
}

// and so on...

It is unclear exactly why you need to ascertain that each field contains a not-null value.不清楚为什么需要确定每个字段包含非空值。 You never posted any code that shows how you intend to use these values.您从未发布任何显示您打算如何使用这些值的代码。

//here i would add the value (0 for null, 1 for data) to an array, which i would handle in the view

Generally, it's better to simply ensure that your view can handle nulls , as this means you don't need to worry about your code executing when a certain value is null.通常,最好简单地确保您的视图可以处理 nulls ,因为这意味着您无需担心在某个值为 null 时执行的代码。

If that is not possible, then it's still easier to simply convert null values into a default not-null value.如果这是不可能的,那么简单地将null值转换为默认的非空值仍然更容易。 This means you still don't have to worry about nulls (after you've converted them) since you won't have any null values anymore.这意味着您仍然不必担心空值(在您转换它们之后),因为您将不再有任何空值。

A simple way to do this is by using the null coalescing operator, eg一个简单的方法是使用空合并运算符,例如

var firstName = element.D1 ?? String.Empty;

This ensures that null values get replaced by a default value (which you can specify, I just chose an empty string here but you can pick whatever you want).这确保null值被替换为默认值(您可以指定,我只是在这里选择了一个空字符串,但您可以选择任何您想要的)。

Also note that you don't even need to do this for non-nullable values such as int , as these will default to 0 automatically.另请注意,您甚至不需要为不可空值(例如int执行此操作,因为这些值将自动默认为0

//here i would add the value to an array

Why use an array instead of an object?为什么使用数组而不是对象? It's unclear why you are deadset on manually handling every property (and its value) instead of passing the object by itself, which is far and away the better OOP way of passing data.目前尚不清楚为什么您对手动处理每个属性(及其值)而不是通过自身传递对象感到死板,这无疑是传递数据的更好的 OOP 方式。

It's contradictory to at the same time want to handle all data manually and also not want to have to handle all that data.既想手动处理所有数据又不想处理所有数据是矛盾的。

I think if you explain more precisely what do you want to do in your view we will be able to help you better.我想如果你更准确地解释你想做什么,我们将能够更好地帮助你。

When i see you code, i deduce that your table in your view will display just one record with his appropriate properties/columns.当我看到您编写代码时,我推断您视图中的表将仅显示一条具有适当属性/列的记录。 If you want to do that, its ok.如果你想这样做,没关系。

On the other hand, iterate over a table according to the number of columns is a bad idea because your are coupling two abstraction levels.另一方面,根据列数迭代表是一个坏主意,因为您正在耦合两个抽象级别。 What i mean is that your MVC Method should NOT have knowledge of the number of columns and this is what your are doing with "for(int i = 1; i < 100; i++)".我的意思是你的 MVC 方法不应该知道列数,这就是你正在做的“for(int i = 1; i < 100; i++)”。 Indeed, what happen if there is a table schema update and the number of columns changes ?确实,如果表模式更新并且列数发生变化会发生什么? The same for the name of the columns ?列的名称相同吗? Your code will be break !你的代码会被破解!

I think you are taking the problem from the wrong side to me.我认为你把问题从错误的方面拿给了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM