4

I am having trouble setting up this Dynamic Linq Library so i can use Dynamic where clauses. Can someone advise me onhow to add this library to my project and reference correctly.

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

also seen in the post

Is there a pattern using Linq to dynamically create a filter?

thanks,

Update -

var x = ListofObjects.AsQueryable().Where("Some comparison");

Update -

After adding the Dynamic.cs library my project wont build with a bunch of compilation errors coming from that specific class. all similar to.

The namespace 'System.Linq.Dynamic' already contains a definition for 'DynamicOrdering' 
Community
  • 1
  • 1
kds6253
  • 835
  • 1
  • 12
  • 17
  • 3
    And now we ha have a new meaning for good old DLL – H H Jan 04 '12 at 13:41
  • (1) Download the library using the link in the Scott's post, (2) Right-click your project/Add reference..., choose "Browse" tab, find the DLL you downloaded, and add it to your project. – Sergey Kalinichenko Jan 04 '12 at 13:42
  • 1
    there is no dll, its just class files. I saw somewhere about adding the Dynamic.cs class and I've tried that but i cannot get it to allow the new functionality it is supposed to support. – kds6253 Jan 04 '12 at 13:45
  • 1
    @kds6253 - After you add the .cs file to your project, be sure to include a `using System.Linq.Dynamic;` in any other file that you want to reference it from. – M.Babcock Jan 04 '12 at 13:49
  • I have done that, maybe i am doing something wrong in my code. I am trying to use the dynamic where on a list of objects. his example on his site is for Linq to SQL. Does anyone have an example of Linq to objects using this. – kds6253 Jan 04 '12 at 13:53
  • @kds6253 - Can you show some code that exemplifies the issue? – M.Babcock Jan 04 '12 at 13:56
  • i have added it to the question. – kds6253 Jan 04 '12 at 13:59
  • that is not full code. Please show a minimal but complete sample that illustrates the problem. – jeroenh Jan 04 '12 at 14:04
  • that is the problem, i need to use the dynamic linq library. this one line of code should be all that is needed, however my syntax must be wrong or i may be misunderstanding how it is used. His example shows something very similar but with an sql db and i want to do it with a list of objects. Linq to objects – kds6253 Jan 04 '12 at 14:08
  • @kds6253 I'm sure I don't understand the question but you're unable to use the linq where() method? is that correct? – user1231231412 Jan 04 '12 at 14:21
  • Yes. I have narrowed it down to the Dynamic.cs class no compiling correctly in my project. 3.5 sp1 – kds6253 Jan 04 '12 at 14:25

1 Answers1

0

Seems to work fine for me:

public class SomeType
{
    public string var1;
    public string var2;
}

class Program
{
    static void Main(string[] args)
    {
        var myList = new List<SomeType>();

        myList.Add(new SomeType() { var1 = "abc", var2 = "abc" });
        myList.Add(new SomeType() { var1 = "def", var2 = "def" });

        foreach (var item in myList.AsQueryable().Where("var1=\"abc\""))
            Console.WriteLine("item.var1 = " + item.var1);
    }
}
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • Turns out my mistake was that i didnt build the project after adding the library. Now that i have added it and tried to build first i am getting a bunch of comilation errors on the Dynamic.cs class which was added. please see update up top. – kds6253 Jan 04 '12 at 14:20
  • I am using the 'DynamicLibrary.cs' file from the site you referenced and it compiles fine. What version of the framework are you compiling against? – M.Babcock Jan 04 '12 at 14:23
  • I just recompiled my test program to run against the 3.5 framework and still do not get any compilation errors (and it still runs fine). Did you copy and paste the code from DynamicLibrary.cs to a newly created Dynamic.cs class file? If so did you do it twice? – M.Babcock Jan 04 '12 at 14:28
  • 2
    i removed and re added the file and using statement and all seems to be working. Sorry about wasting your time. But i appreciate the help. – kds6253 Jan 04 '12 at 14:28