【问题标题】:how to use linqkit within for loop in asp.net core如何在asp.net核心的for循环中使用linqkit
【发布时间】:2020-05-19 14:16:09
【问题描述】:

我的代码在数组长度==1 时有效。 当我的数组长度大于 1 时,我得到错误的列表数据。

    var classtime = PredicateBuilder.True<ServiceManual>();
            for (int i = 0; i < Category.Length; i++)
            {
                long? cateID = Convert.ToInt64(Category[i]);
                if (Category.Length == 1)
                {
                    classtime = classtime.And(i => i.CategoryID == cateID);
                }
                else
                {
                    classtime = classtime.Or(i => i.CategoryID == cateID);
                }
            }
var lstclasssCate = context.tblServiceManual.Where(classtime.Compile()).ToList();

【问题讨论】:

  • 您是否知道在 Category.Length==1 和 Category.Length !=1 时添加了相同的 And 子句
  • 是的,但我使用的是课堂时间。或者,我也得到了错误的列表数据。我更新代码
  • 只是一个提示,当您重用相同的变量名时,阅读代码会更加困难,请考虑更改 classtime.And(i =&gt; i.CategoryID == cateID): 以使用其他变量名而不是迭代变量,例如classtime.And(ct =&gt; ct.CategoryID == cateID);
  • 错误列表数据是什么意思?您是否尝试过调试代码以确保 classTime.compile 的结果包含预期的条件?
  • 意味着我正在获取所有列表数据而不是这些 ID 数据。

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-core asp.net-web-api


【解决方案1】:

你的逻辑是错误的,代码无法编译(在循环中引用 i 已经在使用 i),而且你这样做很困难。

就这样称呼吧:

var cats = Category.Select(c=>Convert.ToInt64(c)).ToArray();
var lstclasssCate = context.tblServiceManual
  .Where(t=>cats.Contains(t.CategoryID))
  .ToList();

艰难的道路:

var classtime = PredicateBuilder.False<ServiceManual>();
foreach(var catId in Category.Select(c=>Convert.ToInt64(c)))
{
  classtime = classtime.Or(i => i.CategoryID == catId);
}
var lstclasssCate=context.tblServiceManual
  .Where(classtime.Compile())
  .ToList();

【讨论】:

    【解决方案2】:

    我用过asp.net core 2.2和LinqKit 1.1.7,可以显示所有列表数据。

    1.型号:

    public class ServiceManual
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    2.Controller(你不能在linq中使用i):

    public class ServiceManualsController : Controller
    {
        private readonly MVC2Context _context;
    
        public ServiceManualsController(MVC2Context context)
        {
            _context = context;
        }
    
            // GET: ServiceManuals
        public List<ServiceManual> Index()
        {
            var Category = new int[] { 1,2,3};
            var classtime = PredicateBuilder.True<ServiceManual>();
            for (int i = 0; i < Category.Length; i++)
            {
                long? cateID = Convert.ToInt64(Category[i]);
                if (Category.Length == 1)
                {
                    classtime = classtime.And(c => c.Id == cateID);
                }
                else
                {
                    classtime = classtime.Or(c => c.Id == cateID);
                }
            }
            var lstclasssCate = _context.tblServiceManual.Where(classtime.Compile()).ToList();
            return lstclasssCate;
        }
    

    3.结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多