Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created February 6, 2020 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/3e6fff415f6886c1b3c3b50b377fa0fc to your computer and use it in GitHub Desktop.
Save dcomartin/3e6fff415f6886c1b3c3b50b377fa0fc to your computer and use it in GitHub Desktop.
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