首页 > 解决方案 > MVC Core 2 - lambda 表达式中的 && 运算符不起作用

问题描述

Visual Studio - 使用 Microsoft ASP.NET 控制器的 MVC Core2

我正在尝试根据用户过滤记录,如果它被标记为删除(布尔数据类型)

我正在使用 && 运算符并尝试按日期列排序,但是它不起作用

这是我的代码

var DataContext = _context.Shops_Basket.Include(c => c.products)
                .Where(c => c.Username == user && c.IsDeleted == 0)).Orderby dates desc;

标签: c#linqlambda

解决方案


Orderby dates desc不是有效的 C#。可能您对 SQL 感到困惑。您需要使用OrderByDescending

var dataContext = _context.Shops_Basket.Include(c => c.products)
    .Where(c => c.Username == user && c.IsDeleted == 0)
    .OrderByDescending(c => c.dates);

另请注意,我们在 C# 中有命名约定。局部变量以较低的字符开头。Shops_Basket我们通常不使用下划线。取而代之的是更好的DbSet名字ShopsBasket


推荐阅读