1

我正在构建一个 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;
}

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

有什么方法可以根据获得的值动态构建对我的数据库的查询,以便我可以只返回我正在寻找的值?

* 编辑 *

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

*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

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

* 还*

请记住,这就是我现在构建应用程序的方式,但如果有更好的做事方式,我愿意接受任何建议。

编辑 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用一个字符串在字典中传递第一个参数,我会得到这个:

核心价值

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;

如何使用字符串进行这样的查询调用?

4

1 回答 1

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());

或使用正则表达式

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)));
于 2013-04-16T16:11:57.017 回答