简体   繁体   English

.OrderBy() / .OrderByDescending() 和 .FirstOrDefault()/.First()

[英].OrderBy() / .OrderByDescending() with .FirstOrDefault()/.First()

I have list of results from table named "product".我有名为“product”的表的结果列表。

Id      User    ProductName     ArrivalDate
----    -----   ------------    ------------
1       James   Shoes           05/07/2016
2       Jenny   TShirt          05/01/2018
3       James   Pants           13/05/2017

i would like to sort the result by descending order based on ArrivalDate where User is "James", which mean it should return to me the third row result.我想根据 ArrivalDate 按降序对结果进行排序,其中 User 是“James”,这意味着它应该将第三行结果返回给我。 However, if i do as below:但是,如果我这样做:

return List(spec).OrderByDescending(x=>x.ArrivalDate).FirstOrDefault();

the result i got is still the first one, appereciate if anyone could guide on this.我得到的结果仍然是第一个,如果有人可以对此进行指导,请感谢。 Below is the sample code:下面是示例代码:

public class EfRepository<T> : IRepository<T>, IAsyncRepository<T> where T : BaseEntity
{
    public T GetSingleBySpec(ISpecification<T> spec)
    {
        return List(spec).FirstOrDefault();
    }
}

public class ProductSpecification : BaseSpecification<NewProducts>
{
    public ProductSpecification(string User)
    : base(b => b.User == User) //User = James
    {

    }
}

public class ProductService : IProductService
{
    public void getOrderProductDetails
    {
        var data = _productRepository.GetSingleBySpec(new ProductSpecification(user));
    }
}

I don't see a filter for the user and you are ordering by user.我没有看到用户的过滤器,您正在按用户订购。 Try this.尝试这个。

return List(spec).Where(x => x.User == "James")
                 .OrderByDescending(y => y.ArrivalDate)
                 .FirstOrDefault();

如果您只想获取特定用户上次到达时间,您应该向 where 子句 FirstOrDefault 添加额外条件,生成如下查询:

  Select top 1 ....

It seems to me that you are trying to encapsulate some basic Linq functionality using your own methods, but you are only complicating things, at the same time you are making it limited and hard to use, instead of flexible and easy to use.在我看来,您正在尝试使用自己的方法封装一些基本的Linq功能,但您只是在使事情复杂化,同时您使其受限且难以使用,而不是灵活且易于使用。

This is what I would do instead:这就是我会做的事情:

public void getOrderProductDetails
{
    var data = _productRepository
        .Where(x => x.User == user)
        .OrderByDescending(x => x.ArrivalDate)
        .FirstOrDefault();
    // process data ...
}

You can also put the "filter" ~inside the FirstOrDefault method.您还可以将“过滤器”~放在 FirstOrDefault 方法中。

I show a generic example (both ways) below.我在下面展示了一个通用示例(两种方式)。

Also note the string-compare for case insensitivity.还要注意不区分大小写的字符串比较。

ICollection<Employee> employees = (blah blah blah);


Employee firstEmployeeWhereClause = employees.Where(z => z.LastName.Equals("Smith", StringComparison.OrdinalIgnoreCase)).OrderByDescending(x => x.EmployeeSurrogateKey).FirstOrDefault();


Employee secondEmployeeNoWhereClause = employees.OrderByDescending(x => x.EmployeeSurrogateKey).FirstOrDefault(z => z.LastName.Equals("Smith", StringComparison.OrdinalIgnoreCase));

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

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