I'm working on an MVC app. A lot of the views are search engine like, allowing the user to select parameters to get the data he wants.
I am looking for an efficient way to make dynamic calls to the database so that I can retrieve only the wanted data instead of taking a bunch of data and sorting them out.
So far I have been using Dynamic Linq, but I have had huge troubles working with this and I was wondering if there was anything better and less troublesome. The only factor is that I know the fields of the table I'm looking on, and I need to be able to use operators like >
, <
or =
.
EDIT
Here's a table example based on my app:
TABLE CARD
(
CARD_IDE INT NOT NULL IDENTITY,
CARD_NAME VARCHAR(50) NOT NULL,
CARD_NUMBER NUMERIC(4) NOT NULL,
CARD_COLOR VARCHAR(10),
CARD_MANA_COST VARCHAR(30),
CARD_MANA_CONVT VARCHAR(3),
CARD_TYPE VARCHAR(50),
CARD_POWER VARCHAR(2),
CARD_TOUGH VARCHAR(2),
CARD_RARTY VARCHAR(1) NOT NULL,
CARD_TEXT_ABILT VARCHAR(800),
CARD_TEXT_FLAVR VARCHAR(800),
CARD_ARTST_NAME VARCHAR(100),
CARD_SET_IDE INT NOT NULL,
CARD_FLAG_FACE INT NOT NULL DEFAULT 0,
CARD_CHILD_IDE INT,
CARD_MASTER_IDE INT,
CARD_UNIT_COST NUMERIC(5,2) NOT NULL DEFAULT 0
)
And a few examples:
- A user look for any item which type is
"Creature" (String),
number is3
and card set IDE is6
; - Any cards which contains the word
Rat
; - All the cards of
color
Blue
andWhite
which unit cost ishigher than 3.00
- Any cards which
power
is less than3
but higher than1
.
EDIT 2
After much research (and thanks to Chris Pratt below), I've managed to look a bit and dive into something.
Based on this:
First I create my object context like this:
var objectContext = ((IObjectContextAdapter) mDb).ObjectContext;
Then I create the ObjectSet:
ObjectSet<CARD> priceList = objectContext.CreateObjectSet<CARD>();
Then I check if any values as been chosen by the user:
if (keyValuePair.Key == CARDNAME)
{
queryToLoad = TextBuilder.BuildQueryStringForTextboxValue(keyValuePair.Value);
//valuesToUse.Add("CARD_NAME.Contains(\"" + queryToLoad + "\")");
priceList = priceList.Where(_item => _item.CARD_NAME.Contains(queryToLoad)) as ObjectSet<PRICE_LIST>;
}
Where the queryToLoad is, in fact, the value to look for. Example: if my user search for an Angel, the queryToLoad will be "Angel". I'm trying to get to the result without having to rewrite my whole code.
And then I gather the result in a List like this:
listToReturn.AddRange(priceList.ToList());
HOWEVER: I have a problem using this approach. As the priceList = priceList.Where(_item => _item.CARD_NAME.Contains(queryToLoad)) as ObjectSet<PRICE_LIST>;
like is struck, the value is always null and I don't know why.