Creating Queries with dynamic column name

38 views
Skip to first unread message

Marcel

unread,
Dec 11, 2014, 6:01:18 AM12/11/14
to simpl...@googlegroups.com
Hello everyone,

I have some requirement which needs to select and update columns that are only known at runtime.

For Example:

var foobar = "Test"
var objectsNoIdIpFound = _db.Workstat.FindAll(
                                        //ID_1 might also be ID_2 in another environment or the value of foobar
                                        _db.Workstat.ID_1 == null &&
                                        _db.Workstat.TCPIP == (deviceObjects.Select(x => x.Value.IP)));

or
//Maybe I don't want to search for AWS_NUM, but for WS_NUM or whatever
 var ws = _db.Workstat.FindAll(_db.Workstat.AWS_NUM == id).First();

Is it possible to replace the Column expression with something generic, so it takes the Name from maybe a string?

Thanks in advance and best regards
Marcel

Richard Wilde

unread,
Dec 11, 2014, 6:32:56 AM12/11/14
to simpl...@googlegroups.com

You can use square braces e.g.

 

var column = "Id";

var person = db.Person.Find(db.Person[column] == 107024);

 

You can even use square braces for a table name if you wish…

 

HTH

Rippo

--
You received this message because you are subscribed to the Google Groups "Simple.Data" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simpledata+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcel Cremer

unread,
Dec 11, 2014, 9:28:27 AM12/11/14
to simpl...@googlegroups.com
Thanks Richard,

I found out that this works for the queries itself. If somebody wants to know what to do, when you got a record:

var ticket = Program.Db["Tablename"].Select(Program.Db["Tablename"].SomeProperty == 1).First();
var dicTicket = (IDictionary<stringobject>)ticket; var assignee = dicTicket["someColumnName"];
The casting to a dictionary does the trick here, because otherwise it will throw an exception saying that you can't access the member with [] braces (I hope the translation is ok).

Now I can access the tables/columns dynamically in the queries and result set.

Note: This one is case sensitive!

Thanks again and have a nice day
Marcel

--
You received this message because you are subscribed to a topic in the Google Groups "Simple.Data" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/simpledata/RkyOSiXwqpU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to simpledata+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Mit freundlichen Grüßen

Marcel Cremer
Leimbachweg 2
51069 Köln
Tel.:     +49 (0) 221 / 29437765
Mobil.: +49 (0) 151 / 26437712
Fax.:    +49 (0) 221 / 96988758
E-Mail: m.cre...@googlemail.com
Reply all
Reply to author
Forward
0 new messages