linq 如何使用多选下拉值组合筛选记录

jgwigjjp  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(158)

我有一个多选下拉菜单,用户可以选择一个以上的项目在同一时间。使用这个多选值,我需要过滤我的数据源,并将解析结果返回给gridview。这是我的代码。我已经尽力解释了。请随时问我任何问题。我也添加了代码。我想我马上就要解决这个问题了,但也许有一个更好更容易的方法来解决这个问题。先谢谢你了
预期方案和结果(根据所选部门返回非空值)

给出了多选场景
生产-1,运输-2,物流-3,根据下拉列表

  • Production ----> Return all not null value for ProductionError
  • Logistics ----->返回LogisticsError的所有非空值
  • Shipping ----->返回ShippingError的所有非空值
    多选组合时
  • Production + Shipping -> Return all not null for(ProductionError & Shipping)
  • Production + Logistics-> Return all not null for(ProductionError & LogisticsError)
  • 运输+物流,即(运输和物流不为空)
  • Shipping + Logisitics + Production(对于Shipping & Logisitics & ProductionErrors,非空)
    我的代码到目前为止:)
  • 主方法具有上面定义的场景I。
  • farmerlist有我们需要过滤的记录。
  • 如果语句可能需要更改
internal class Program
    {
        static void Main(string[] args)
        {
            //Lets say user selected 3 values in the multiselect
            string dropdownSeleted = "1,2,3";
            var result = new List<Farmers>();   

            foreach (string dropdownSeletedValue in dropdownSeleted.Split(','))
            {
                //check if the enum type matches 
                if (DropdownEnum.Production.ToString() == dropdownSeletedValue)     
                {
                    farmerList()
                    result =  farmerList().Where(x => x.ProductError != null).ToList();

                }

                //When user selects 2 
                if (DropdownEnum.Shipping.ToString() == dropdownSeletedValue)
                {

                    result =farmerList().Where(x => x.ShippingError != null).ToList();

                }

                //When user select 3 
                if (DropdownEnum.Logistics.ToString() == dropdownSeletedValue)
                {

                    result = farmerList().Where(x => x.ProductError != null).ToList();

                }
                

            }
            // The above approach is not working if we have multi select like below 
            //Need to do 

            //When user selects 1&2 i.e Production & Logistics             
            //When user selects 1&3 i.e Production & Logistics 
            // When user selects 2&3 i.e Shiping and Logistics 

            //when user select 1,2,3 
             result = farmerList().Where(x => x.ProductError != null).Where(x => x.ShippingError != null).Where(x => x.LogisticError != null).Select(x => x).ToList();  //returns Id = 1 which is right , maybe i can use this and add if to it 

            //Do I do this for each combinations or is there a better approach to doing this 
        }

        public static List<Farmers> farmerList()
        {
            string dropdownSeletedValue = "0,1"; // here the user selected 0- Production  1-Shipping 
            //Idea is to show the records for multiselected Items 

            List<Farmers> resultForDataSouuce = new List<Farmers>();
            List<Farmers> farmers = new List<Farmers> {new Farmers {ID = 1, Name = "Item1",  ProductError = 1 ,    ShippingError = 1 ,    LogisticError = 2},
                                                          new Farmers {ID = 2, Name = "Item2",  ProductError = null , ShippingError = 3,     LogisticError = 2},
                                                          new Farmers {ID = 3, Name = "Item3",  ProductError = null , ShippingError = null,  LogisticError = null },
                                                          new Farmers {ID = 4, Name = "Item4",  ProductError = null , ShippingError = null,  LogisticError = null },
                                                          new Farmers {ID = 5, Name = "Item5",  ProductError = 1 ,    ShippingError = null,  LogisticError = null },
                                                          new Farmers {ID = 6, Name = "Item6",  ProductError = 1 ,    ShippingError = null,  LogisticError = null },
                                                          new Farmers {ID = 7, Name = "Item7",  ProductError = 3 ,    ShippingError = null,  LogisticError =4},
                                                          new Farmers {ID = 8, Name = "Item8",  ProductError = null , ShippingError = 5,     LogisticError = 5}
                                                         };
          
            return farmers;
        }

    }

    public enum DropdownEnum
    {
        Production = 1,
        Shipping = 2,
        Logistics = 3,
    }

    public class Farmers
    {
        public int ID
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string Location
        {
            get;
            set;
        }
        public double? LogisticError
        {
            get;
            set;
        }

        public int? ProductError 
        {
            get;
            set;
        }

        public int? ShippingError
        {
            get;
            set;
        }
    }
lkaoscv7

lkaoscv71#

在考虑了几个小时之后。意识到需要扩展IEnumerable之上的扩展Where方法,以获取<condition,predicate>,这将解决答案。因此写了一个新的扩展方法WhereIf。

string dropdownSeletedValue = "0,1,2"; 
//this is also given where we can compare the string with the selected Value from the Dropdown. 
 public enum DropdownEnum:int
    {
        Production = 1,
        Shipping = 2,
        Logistics = 3,
    }

我可以使用contains来满足条件,即参数1,并将 predicate 作为参数2传递。

var retVal = farmerList.WhereIf(dropdownSeletedValue.Contains(((int)(DropdownEnum.Production)).ToString()), y => y.ProductError != null)
                               .WhereIf(dropdownSeletedValue.Contains(((int)(DropdownEnum.Shipping)).ToString()), y => y.ShippingError != null)
                               .WhereIf(dropdownSeletedValue.Contains(((int)(DropdownEnum.Logistics)).ToString()), y => y.LogisticError != null);

你可能会问我去了哪里,在列表上的哪里,这就是怎么做的。创建一个类作为helper方法。

  • 第二个是<condition,predicate>,它是在IEnumerable之上使用的,请记住这一点。
public static class LinqHelper
        {
            public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Expression<Func<TSource, bool>> predicate)
            {
                if (condition)
                return source.Where(predicate);
            else
                return source;
        }

        public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate)
        {
            if (condition)
                return source.Where(predicate);
            else
                return source;
        }

        public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, int, bool> predicate)
        {
            if (condition)
                return source.Where(predicate);
            else
                return source;
        }
    }

Your Welcome ! :)

相关问题