1

私はC#で次の文字列を持っています

Count([AssignedTo]) as [AssignedTo] , Sum([Billing Amount]) as [Billing Amount] , Max([Billing Rate]) as [Billing Rate] , Min([ExecutionDate]) as [ExecutionDate] , Average([HoursSpent]) as [HoursSpent] , [Project], [Sub-Project], [TaskName], [Vendor], [Work Classification], [Work Done], Count([WorkItemType]) as [WorkItemType]  

今、文字列操作または集計関数を持つすべてのフィールドのリストが必要ですlinq

のような出力

Count([AssignedTo])

 Sum([Billing Amount])

 Max([Billing Rate])

 Min([ExecutionDate])

 Average([HoursSpent]) 

Count([WorkItemType])
4

3 に答える 3

2

おそらくこれはあなたのために働く:

var aggr = new []{ "Count", "Sum", "Max", "Min", "Average"};
var allAggregates = text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(col => new{ col, token = col.TrimStart().Split().First() })
    .Where(x => x.token.Contains('(') && aggr.Any(a => x.token.StartsWith(a, StringComparison.OrdinalIgnoreCase)))
    .Select(x => x.token);

DEMO

関数内にあるフィールド名のみを取得できますか

可能であれば、正規表現ではなく文字列メソッドを好みます。

var allAggregates = text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(col => new { col, token = col.TrimStart().Split().First() })
    .Where(x => x.token.Contains('(') && aggr.Any(a => x.token.StartsWith(a, StringComparison.OrdinalIgnoreCase)))
    .Select(x => {
        string innerPart = x.token.Substring(x.token.IndexOf('(') + 1);
        int index = innerPart.IndexOf(')');
        if (index >= 0)
            innerPart = innerPart.Remove(index);
        return innerPart;
    });
于 2013-08-30T11:04:36.387 に答える
0
var aggregates = new []{ "Count", "Sum", "Max", "Min", "Average"};
var output=Regex.Matches(input,@"(\w+)\(.*?\)")
                .Cast<Match>()
                .Where(x=>aggregates.Any(y=>y==x.Groups[1].Value))
                .Select(z=>z.Value);
于 2013-08-30T11:12:30.490 に答える
0

これは別の方法です:

string[] funcNames =  new string[]{"Sum","Average","Count","Max","Min"};
string s = "Your String";

var output = from v in s.Split(',')  
             where funcNames.Contains(v.Split('(')[0].Trim())
             select v.Split(new string[]{" as "},  
                            StringSplitOptions.RemoveEmptyEntries)[0].Trim();
             .Split('(')[1].Split(')')[0]; //this line is to get only field names

//Print results
foreach(var str in output) Console.WriteLine(str);
于 2013-08-30T11:25:18.667 に答える