Created
February 6, 2020 16:23
-
-
Save dcomartin/3e6fff415f6886c1b3c3b50b377fa0fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Dynamic; | |
using Xunit; | |
namespace FullCircleTMS.Tests.Shared | |
{ | |
public class DynamicLinqTests | |
{ | |
[Fact] | |
public void Test() | |
{ | |
var list = new List<Entity> | |
{ | |
new Entity | |
{ | |
IsActive = true, | |
IsSomething = 1 | |
}, | |
new Entity | |
{ | |
IsActive = false, | |
IsSomething = 1 | |
}, | |
new Entity | |
{ | |
IsActive = true, | |
IsSomething = 2 | |
}, | |
new Entity | |
{ | |
IsActive = false, | |
IsSomething = 2 | |
}, | |
}.AsQueryable(); | |
var linqResult = list.Where(o => (o.IsActive && o.IsSomething == 1) || (o.IsActive == false && o.IsSomething == 2)).ToArray(); | |
Assert.Equal(2, linqResult.Length); | |
var dynamicResult = DynamicQueryable.Where(list, "o => (o.IsActive && o.IsSomething == 1) || (o.IsActive == false && o.IsSomething == 2)").ToArray(); | |
Assert.Equal(2, dynamicResult.Length); | |
} | |
} | |
public class Entity | |
{ | |
public bool IsActive { get; set; } | |
public int IsSomething { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment