Building a Dynamic Linq Query using Equals and an Array

c# dynamic-linq linq where

Question

I have been trying to solve the syntax for a dynamic linq query that is needed in my application.

I have a dynamic query that the where clause needs to be specified to either

  • GuidPrimaryKey is contained in a list of Guid OR
  • GuidPrimaryKey is equal to an item in a list of Guid (using some type of for-loop)

I have a Guid[] populated with over 5,000 keys. My Query is set up as

If I do this (as a test) it is successful

data = data.where("GuidPrimaryKey.Equals(@0)",array[0]);

as well as

data = data.where("GuidPrimaryKey.Equals(@0) OR GuidPrimaryKey.Equals(@1)",array[0], array[1]);

I have tried:data = data.where("GuidPrimaryKey.Contains(@0)",array); but that gives an error: No applicable method 'Contains' exists in type 'Guid'.

I also tried setting a loop to go through the elements in the array and set the where clause as a giant string, but that did not work either.

string s = "";
string p = ""
int counter = 0;
foreach(Guid g in Array)
{
s+= "GuidPrimaryKey.Equals(@" counter.ToString() + ") OR";
p += "Array[" counter.ToString() + "],";
counter++;
}

s = s.remove(s.length - 3, 3);
p = p.remove(p.length - 1, 1);

data = data.Where(s,p);

This gives me the error message: No Property or field '1' exists in type 'DynamicClass1'

Any ideas? I need to have the where clause build the query to check to see if the primary key (GuidPrimaryKey) exists in the list of keys (Guid[]).

1
1
6/19/2014 1:13:05 PM

Popular Answer

I'm not sure if this works in the standard Dynamic Linq library, but I just tried this is my open-source version, and it works well:

var data = data.Where("GuidPrimaryKey in @0", array);

This also works:

var data = data.Where("@0.Contains(GuidPrimaryKey)", array);

Here is a full unit test I wrote to confirm this:

[TestMethod]
public void ExpressionTests_ContainsGuid()
{
    //Arrange

    //Generate some users with Id fields of type Guid
    var userList = User.GenerateSampleModels(5, false); 
    var userQry = userList.AsQueryable();

    //Generate a list of values that will fail.
    var failValues = new List<Guid>() { 
        new Guid("{22222222-7651-4045-962A-3D44DEE71398}"), 
        new Guid("{33333333-8F80-4497-9125-C96DEE23037D}"), 
        new Guid("{44444444-E32D-4DE1-8F1C-A144C2B0424D}") 
    };

    //Add a valid Guid so that this list will succeed.
    var successValues = failValues.Concat(new[] { userList[0].Id }).ToArray();


    //Act
    var found1 = userQry.Where("Id in @0", successValues);
    var found2 = userQry.Where("@0.Contains(Id)", successValues);
    var notFound1 = userQry.Where("Id in @0", failValues);
    var notFound2 = userQry.Where("@0.Contains(Id)", failValues);

    //Assert
    Assert.AreEqual(userList[0].Id, found1.Single().Id);
    Assert.AreEqual(userList[0].Id, found2.Single().Id);
    Assert.IsFalse(notFound1.Any());
    Assert.IsFalse(notFound2.Any());
}
1
7/11/2014 9:23:02 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow