I'm building an MVC app and, in this app, I need to get a list of objects based on filters used by the user.
So I might end up having a string like this:
*OBJNAME=[SWORD][OF][THOUSAND][TRUTHS]*OBJTYPE=[ARTEFACT]*OBJNUMBER=[28]*COST=[9]+<*POWER=[3]+>=*RATING=[4]+<*OWNER=[WRIGHT][STAN]*OBJSET=[WORLD OF WARCRAFT]*RARITY=[LEGENDARY]*ADDCOST=[6QGBA]*OBJCOLOR=[ALL]
Every * is a tag I introduce when building the string so that I may identify each attributes separately in the DAL class:
public List<ObjInfo> ListObjWithQryString(string _objQry)
{
List<ObjInfo> listToReturn = new List<ObjInfo>();
char[] firstDelimiters = {'*'};
string[] parsedValues = _objQry.Split(firstDelimiters,
StringSplitOptions.RemoveEmptyEntries);
foreach (string parsedValue in parsedValues)
{
if (parsedValue.Contains("OBJNAME"))
{
// WRITE CODE HERE
}
}
return listToReturn;
}
Now my problem is based on the fact that each parameter may OR may not be there, so the query string will change, and I don't want to have to deal with each 81 possibilities, especially since that may and will change eventually.
Is there any way to dynamically build a query to my database depending on the values obtained so that I may return only the value I am looking for?
* EDIT *
Here are a couple of examples of strings that I may end up having:
*OBJNAME=[SWORD][OF][THOUSAND][TRUTHS]
// Seeking all objects named Sword of a Thousand Truths
*OBJNAME=[SWORD][OF][THOUSAND][TRUTHS*OBJNUMBER=[28]*COST=[9]+<*POWER=[3]+>=*RATING=[4]+<RARITY=[LEGENDARY]*ADDCOST=[6QGBA]*OBJCOLOR=[ALL]
// Seeking all objects named Sword of a Thousand Truths with number 28 with cost lesser than 9 with power greater or equal to 3 with rating lesser than 4 with rarity Legendary with additional cost 6QGBA with all colors
*OBJNAME=[SWORD][OF][THOUSAND][TRUTHS]*OBJTYPE=[ARTEFACT]*OBJNUMBER=[28]*COST=[9]+<*POWER=[3]+>=*RATING=[4]+<*OWNER=[STAN]*OBJSET=[WORLD OF WARCRAFT]
// Seeking all objects named Sword of a Thousand Truths with type Artefact with number 28 with cost lesser than 9 with power greater or equal to 3 with owner Stan with objSet World of Warcraft
*ADDCOST=[6QGBA]*OBJCOLOR=[ALL]
// Seeking all objects with additional cost 6QGBA with all colors
*ADDCOST=[6QGBA]*OBJCOLOR=[BLUE][RED][PURPLE]
// Seeking all objects with additional cost 6QGBA with specific color Blue, Red, Purple
*OBJTYPE=[ARTEFACT]*POWER=[3]+>=
// Seeking all objects with type Artefact and power greater or equal to 3
So, you see that I might end up having different tag to the string. I can paste up additional information if needed.
* ALSO *
Keep in mind that it's how I built my app right now, but I'm open to any suggestion if there's a better way to do things.
EDIT 2
Thanks to @I4V, I have a dictionary that actually groups the values. So:
var dict = Regex.Matches(_cardQry, @"\*(\w+)=([^\*$]+)").Cast<Match>()
.ToDictionary(x => x.Groups[1].Value,
x => string.Join(" ", x.Groups[2].Value.Split(new char[] {'[', ']'})));
Will give a dictionary. If I do a foreach
through the dictionary with a string having the first parameters passed up there, I end up with this:
KEY / VALUE
OBJNAME= SWORD OF THOUSAND TRUTHS
OBJTYPE= ARTIFACT
OBJNUMBER= 28
COST= 9 +>= //(small bug here, there should not be a "=" sign at the end, but it's not major)
POWER= 3 +<=
RATING= 4+<
OWNER= STAN
OBJSET= WORLD OF WARCRAFT
RARITY= LEGENDARY
ADDCOST= 4 W G U G R
OBJCOLOR= ALL
and with this line of code:
var whereCondition = "WHERE " + String.Join(" AND ", dict.Select(kv => kv.Key + "='" + kv.Value + "'"));
I end up having a rather use string that looks like this:
WHERE OBJNAME=' SWORD OF THOUSAND TRUTHS ' AND OBJTYPE=' ARTIFACT ' AND OBJNUMBER=' 28 ' AND COST=' 9 +<' AND POWER=' 3 +>' AND RATING=' 4 +<' AND OWNER=' STAN ' AND OBJSET='WORLD OF WARCRAFT ' AND RARITY=' LEGENDARY ' AND ADDCOST=' 4 W G U G R ' AND OBJCOLOR=' ALL '
Now, the trouble is NOT about building the string, but rather how to use it to make a query call. I am very new to MVC app and, especially, database call. I usually made my database calls like this:
var objQry = from o in m_DB.O
where o.NAME == _nameProvided
select o;
How can I use the string to make such a query call?