dynamic-linq-query-builder icon indicating copy to clipboard operation
dynamic-linq-query-builder copied to clipboard

Using complex Any() conditions

Open paradisehuman opened this issue 3 years ago • 1 comments

Hi. How can I write such a thing?

var Orders = new List<Order>()
            {
                new Order{
                    OrderId =1,
                    CreationTime = DateTime.Now,
                    ProductProviderType = "DomesticFlight"
                },
                new Order{
                    OrderId =2,
                    CreationTime = DateTime.Now,
                    ProductProviderType = "InternationalFlight"
                },
                new Order
                {
                     OrderId =3,
                    CreationTime = DateTime.Now,
                    ProductProviderType = "Manuel"
                },
                new Order
                {
                     OrderId =4,
                    CreationTime = DateTime.Now,
                    ProductProviderType = "Hotel"
                }
            };
            
var hasOffer = Orders.Any(p => p.ProductProviderType == "Hotel") && Orders.Any(p => p.ProductProviderType == "DomesticFlight" || p.ProductProviderType == "InternationalFlight");

I have written something like that but it doesn't have any output :

var myFilter = new QueryBuilderFilterRule()
            {
                Condition = "AND",
                Rules = new List<QueryBuilderFilterRule>()
                {
                    new QueryBuilderFilterRule
                    {
                        Field = "ProductProviderType",
                        Id = "1",
                        Input = "text",
                        Operator = "in",
                        Type = "string",
                        Value = new [] { "Hotel" }
                    },
                    new QueryBuilderFilterRule()
                    {
                        Condition = "OR",
                        Rules = new List<QueryBuilderFilterRule>
                        {
                            new QueryBuilderFilterRule
                            {
                                Field = "ProductProviderType",
                                Id = "2",
                                Input = "text",
                                Operator = "in",
                                Type = "string",
                                Value = new [] { "InternationalFlight" }
                            },
                            new QueryBuilderFilterRule
                            {
                                Field = "ProductProviderType",
                                Id = "2",
                                Input = "text",
                                Operator = "in",
                                Type = "string",
                                Value = new [] { "DomesticFlight" }
                            },
                        },
                    }
                }
            };

paradisehuman avatar Mar 01 '22 14:03 paradisehuman

Id value should be same as Field in your case:

Field = "ProductProviderType", Id = "ProductProviderType",

If the ProductProviderType was a collection of objects, Id value would be different from FIeld:

Field = "ProductProviderType.Prop1Name", Id = "Prop1Name",

I hope this answers your question.

Ibrahemkhalil avatar Mar 04 '22 21:03 Ibrahemkhalil