2

Hey i am making a simple search machine through alot of different coloumns in 2 tables.

I was trying to get this to abit dynamical.

I read this:

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

Which is something that really could do the trick for me.. its just in VB and i need it in c#

here is my code :

private void displayWith1Criteria(string column, string value)
{
    Console.WriteLine("entering _1_ display method");
    dbcontent = new DBtestEntities();
    var studienummerQuery = from members in dbcontent.Medlemmer.Include("Retninger")
                            where column == value
                            orderby members.Fornavn
                            select new { Studienr = members.Studienummer, Fornavn = members.Fornavn, Efternavn = members.Efternavn, Email = members.Email, Studiested = members.Studiested, Betaling = members.BetalingsType, Uddannelses_Retning = members.Retninger.retningNavn };

    dataGridView1.DataSource = studienummerQuery;
}

Doesn't return any data at all...

column is being called with members.Fornavn (Fornavn - a column name)

value = Anders (one of the data's in Fornavn column)

What I want to do: My database is loaded into dbcontent using a .edmx file from ABO entity class. My database consist of 2 tables, "Retninger" and "Medlemmer". Medlemmer contains columns things like Fornavn(in english, Firstname), Efternavn(Lastname), Studienummer(study no.) What i would like is a "dynamic" method that can set both which column to be searched in and the value that needs to be searched for in the set column.

Community
  • 1
  • 1
Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • 1
    I think you need to look into your 'where' clause, you're comparing the two input arguments, which I don't think is your intention? Right now, the only time data will be returned is when the method has been called and passed equal 'column' and 'value' values AND there is data to be had from 'Include'. – Grant Thomas Dec 07 '10 at 16:55
  • Which part of your query do you need to be dynamic? – mcass20 Dec 07 '10 at 16:56
  • well i have like 12 coloumns in my database table. – Anders Metnik Dec 07 '10 at 17:41
  • i would like to be able to search each of them, twice at a time, So i need to be able to change the coloumn aswell as the value which is being searched for – Anders Metnik Dec 07 '10 at 17:43

4 Answers4

2

I think this answer from Shawn Miller to the question you linked is more what you are looking for:

http://www.albahari.com/nutshell/predicatebuilder.html

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • I dont think so, atleast not the way i understand it. I understand it as it is a way to search through alot of text, and finding keywords or partly keywords etc. What i need i to be able to search through different coloumns, which is different types, and so will value ofc also be.. Though i'd only like to do 2 coloumns at a time. Thats why in the post i posted in my main post, the 2nd reply to that (by geoff) looked alot like what i needed. But i dont know how to write that in c# – Anders Metnik Dec 07 '10 at 17:56
2

When could your expression column == value possibly return true? Only if string.Equals("Fornavn", "Anders") is true.

Doing dynamic linq is hard. Is usually do it this way:

...
where (!useMycolumn1 || member.mycolumn1 == value1) 
    &&(!useMycolumn2 || member.mycolumn2 == value2) 
    &&(!useMycolumn3 || member.mycolumn3 == value3) 
...

useMycolumn* is a local boolean variable which is set to true or false, depending on whether the certain condition should be tested or not. This way, unused parts of the query are optimized out at compile time.

codymanix
  • 28,510
  • 21
  • 92
  • 151
  • Im working on my database, where Fornavn is a coloumn of my table. – Anders Metnik Dec 07 '10 at 17:59
  • This coloumn holds alot of firstnames (fornavn, danish), and i f.eks. need to find all firstnames equal to Anders.. Then another time i might need to look at surnames (another coloumn in my DB). etc. is that possible without hardcoding it all? – Anders Metnik Dec 07 '10 at 18:00
0

Are you remembring to call the DataBind() method on the grid? How do you know nothing is being returned?

dexter
  • 7,063
  • 9
  • 54
  • 71
0

I think its because of lazy evaluation of LINQ queries. You can try using .ToList, as below:

dataGridView1.DataSource = studienummerQuery.ToList();

also .DataBind(), if relevant for your object.

Edit:

Lazy Evaluation: This Link, would serve as a good start

codymanix
  • 28,510
  • 21
  • 92
  • 151
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103