从具有不同值的字符串构建查询

时间:2013-04-16 15:58:11

标签: c# asp.net-mvc linq dynamic-queries

我正在构建一个MVC应用程序,在这个应用程序中,我需要根据用户使用的过滤器获取对象列表。

所以我最终可能会得到这样的字符串:

*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]

每个*都是我在构建字符串时引入的标记,以便我可以在DAL类中单独标识每个属性:

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;
}

现在我的问题是基于这样一个事实:每个参数都可能不存在,所以查询字符串会改变,我不想处理每种可能性,特别是因为这可能会改变最终

有没有办法根据获得的值动态构建对我的数据库的查询,以便我只返回我要查找的值?

*编辑*

以下是我可能最终拥有的几个字符串示例:

*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

所以,你看到我最终可能会对字符串使用不同的标记。如果需要,我可以粘贴其他信息。

* ALSO *

请记住,这就是我现在构建应用程序的方式,但如果有更好的方法,我会对任何建议持开放态度。

编辑2

感谢@ I4V,我有一个实际上对值进行分组的字典。所以:

var dict = Regex.Matches(_cardQry, @"\*(\w+)=([^\*$]+)").Cast<Match>()
                            .ToDictionary(x => x.Groups[1].Value,
                                          x => string.Join(" ", x.Groups[2].Value.Split(new char[] {'[', ']'})));

会给一本字典。如果我通过字典用foreach做一个字符串,其中第一个参数传递到那里,我最终会得到这个:

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 

并使用以下代码行:

var whereCondition = "WHERE " + String.Join(" AND ", dict.Select(kv => kv.Key + "='" + kv.Value + "'"));

我最终得到一个相当使用的字符串,如下所示:

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 '

现在,问题不在于构建字符串,而在于如何使用它来进行查询调用。我是MVC应用程序的新手,尤其是数据库调用。我通常像这样进行数据库调用:

var objQry = from o in m_DB.O
             where o.NAME == _nameProvided
             select o;

如何使用字符串进行此类查询调用?

1 个答案:

答案 0 :(得分:1)

您可以从输入字符串创建字典。使用它会更简单。

string input = "*OBJNAME=[SWORD][OF][THOUSAND][TRUTHS]*OBJTYPE=[ARTEFACT]*OBJNUMBER=[28]*COST=[9]+<*POWER=[3]+>=*RATING=[4]+<*OWNER=[WRIGHT][STAN]*OBJSET=[RETURN TO RAVNICA]*RARITY=[LEGENDARY]*ADDCOST=[6QGBA]*OBJCOLOR=[ALL]";

var dict = input.Split(new char[]{'*'},StringSplitOptions.RemoveEmptyEntries)
             .Select(p => p.Split('='))
             .ToDictionary(kv => kv[0], 
                           kv => kv[1].Replace("[", "").Replace("]", " ").Trim());

或使用Regex

var dict2 = Regex.Matches(input, @"\*(\w+)=([^\*$]+)").Cast<Match>()
            .ToDictionary(x => x.Groups[1].Value, 
                          x => String.Join(" ",x.Groups[2].Value.Split(new char[]{'[',']'},StringSplitOptions.RemoveEmptyEntries)));