简体   繁体   English

Linq检查null并按orderby替换null值

[英]Linq check for null and replace null value in orderby

I have a linq query where sometimes the string field, product name, may be empty, null. 我有一个linq查询,有时字符串字段,产品名称可能为空,null。 This is a new field in the table and if data hasn't been entered, then I get an error when running the query. 这是表中的新字段,如果尚未输入数据,则在运行查询时出现错误。 What I need to do is check for null and if null replace the null/empty value with the text value from a drop down list. 我需要做的是检查null,如果为null,则用下拉列表中的文本值替换null / empty值。 I've tried several things but can't get it to work. 我已经尝试了几件事,但无法让它发挥作用。

public IQueryable<Product> BindProduct([Control("ddProductName")] int? ProductNameId)
{
   var prod = from c in _DbContext.Products
      where c.ProductNameId == ProductNameId
      orderby string.IsNullOrEmpty(c.ProductName) ? ddProductName.SelectedItem.Text : c.ProductName,
      c.ItemNumber
      select c;
      return prod;
}

CHANGED TO: 变成:

 public IQueryable<Product> BindProduct([Control("ddProductName")] int? ProductNameId)
    {
        var prodName = ddProductName.SelectedItem.Text;
        var prod = from c in _DbContext.Products
                   where c.ProductNameId == ProductNameId
                   let sortName = c.Name ?? prodName
                   orderby sortName, c.ItemNumber
                   select new { c, sortName };
        return prod;

    }

UPDATE: I don't think I was clear. 更新:我认为我不清楚。 What I need to do is check for null and if null replace the null/empty value with the text value from a drop down list. 我需要做的是检查null,如果为null,则用下拉列表中的文本值替换null / empty值。

"Let" keyword should do the trick. “让”关键字应该可以解决问题。

Here is a LINQPad example: 这是一个LINQPad示例:

var products = Enumerable.Range(0, 100).Select(q => new {ProductNameId = 1, ProductName = (q % 15 == 0 ? null : (q % 3 == 0 ? "Fizz" : (q % 5 == 0 ? "Buzz": q.ToString()))), ItemNumber = q});
var ddProductName_SelectedItem_Text = "FizzBuzz";
var ProductNameId = 1;
var prod = from c in products
                     where c.ProductNameId == ProductNameId
                     let sortName = c.ProductName ?? ddProductName_SelectedItem_Text
                     orderby sortName, c.ItemNumber
                     select c;
return prod; //prod.Dump(); //for linqpad debugging

The answer came from a peer of mine, Brad. 答案来自我的同行布拉德。 For anyone else who is wondering how to replace a null/empty value using linq, see below. 对于其他想知道如何使用linq替换null /空值的人,请参见下文。

public IQueryable<Product> BindProduct([Control("ddProductName")] int? ProductNameId)
{
    var prodName = ddProductName.SelectedItem.Text;
    var prod = _DbContext.Products.Where(c => c.ProductNameId == ProductNameId).ToList().Select
    (p =>
    {
        p.Name = string.IsNullOrEmpty(p.Name) ? prodName : p.Name;
        return p;
    }).OrderBy(p => p.Name).ThenBy(p => p.ItemNumber).AsQueryable();

    return prod;
}

When you call the orderby in your query, LINQ to SQL will attempt to resolve it into an order by statement in SQL. 当您在查询中调用orderby时,LINQ to SQL将尝试将其解析为SQL中的order by语句。 Since SQL has no method for a conditional order by statement, the method will fail to compile from LINQ. 由于SQL没有方法用于条件顺序by语句,因此该方法将无法从LINQ编译。

What you want can be achieved, but it will require ordering the set client side instead of server side. 你想要什么可以实现,但它需要订购设置客户端而不是服务器端。 My suggestion would be to run the query without the order by, and at the point that you need to order, first transform the collection to an IEnumerable collection. 我的建议是在没有顺序的情况下运行查询,并且在您需要订购时,首先将集合转换为IEnumerable集合。 This will pull into memory the results from the IQueryable, and you'll be able to run the orderby against the object collection. 这将从IQueryable中获取结果,并且您将能够针对对象集合运行orderby。

The basic flow will be something like this: queryable.AsEnumerable().OrderBy() 基本流程将是这样的:queryable.AsEnumerable()。OrderBy()

The big takeaway here is to remember that LINQ will try to transform an IQueryable into a SQL query at the point at which you request an enumeration over the collection. 这里最重要的一点是要记住,LINQ会尝试在您请求对集合进行枚举时将IQueryable转换为SQL查询。 There is a pretty good high level explanation here: http://blog.worldmaker.net/2013/mar/11/mini-lecture-linq-visualization/ . 这里有一个非常好的高级解释: http//blog.worldmaker.net/2013/mar/11/mini-lecture-linq-visualization/

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

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