我有一个字符串列表Currency Code
。
var currencyCode = new List<string>() { "USD", "SGD", "KWD", "BHD", "LYD" };
我有另一个复杂的对象。
var rate = new List<Rate>()
{
new Rate() { CurrencyName = "USD (SMALL)",CurrencyCode = "USD SMALL",BranchName="Branch1"},
new Rate() { CurrencyName = "SGD BIG",CurrencyCode = "SGD BIG",BranchName="Branch1"},
new Rate() { CurrencyName = "KUWAIT DINAR",CurrencyCode = "KWD",BranchName="Branch1"},
new Rate() { CurrencyName = "USD BIG (100,50)",CurrencyCode = "USD BIG",BranchName="Branch1"},
new Rate() { CurrencyName = "USD MEDIUM (10,20)",CurrencyCode = "USD MEDIUM",BranchName="Branch1"},
};
我将在下面的列表中找到匹配的货币:
var matchedCurrency = from c in rate
where currency.Any(w => c.CurrencyCode.Contains(w))
select c;
我想要的是匹配的货币列表应该按货币代码分组。
我尝试了以下方式,但没有奏效。
var Grp = rate.GroupBy(item => currency.Any(w => item.CurrencyCode.Contains(w)))
.Select(group => new
{
group.Key,
DataList = group.ToList()
});
我不知道我实际上是在失踪。我已经通过各种方式尝试过。
我知道我可以遍历rate
并推入另一个对象。但是这看起来不太好我想通过使用Linq来做到这一点。但我无法达到这一点。
此对象将显示输出:
public class CurrencyMap
{
public string Code { get; set; }
public List<Currency> currency { get; set; }
}
public class Currency
{
public string CurrencyName { get; set; }
public string CurrencyCode { get; set; }
public string BranchName { get; set; }
}
enter code here
编辑:
我最初错过了这些东西,但如果在费率中找不到匹配的代码,我还需要有空列表。
In Rate中没有&#34; BHD&#34;,&#34; LYD&#34;的匹配列表。但我还需要使用代码&#34; BHD&#34;,&#34; LYD&#34;
的空列表答案 0 :(得分:2)
这是一种相当hacky的方法,但您可以使用Regex.Match来实现此目的。基本的想法是,您需要def naming_of_sequence_neurons():
letters_list = ["a", "b", "c", "d", "e", "f", "g",
"h", "i", "k", "l", "m", "n", "p",
"q", "r", "s", "t", "v", "w", "y"]
input_neuron_web = []
def naming_cycle(number):
for j in range(0, 21):
input_neuron_web[j][number] = Neuron.name = (letters_list[j] + str(number))
for i in range(0, 9):
naming_cycle(i)
return input_neuron_web
inputed = naming_of_sequence_neurons()
for a in range(0, 21):
for b in range(0, 9):
print(inputed[a][b].name)
的值作为分组的关键。
这可以通过与正则表达式的成功匹配来返回。属性Match.Value将包含键的字符串值
免责声明:不幸的是,所有的否定匹配也将作为空组返回。您需要过滤掉空组:
currencyCode
实际上它也没有正则表达式:
var result = currencyCode.SelectMany
(
x=> rate.Where(r=> r.CurrencyCode.Contains(x))
.GroupBy(r=> Regex.Match(r.CurrencyCode, x).Value)
).Where(x=> !string.IsNullOrWhiteSpace(x.Key));
免责声明2:与所有模式匹配一样,如果模式不明确,则会导致问题。如果var result = rate.GroupBy(r => currencyCode.FirstOrDefault(c=> r.CurrencyCode.Contains(c)))
.Where(x=> !string.IsNullOrWhiteSpace(x.Key));
值包含超过1个偏见(可能在单词内),则可以获得非感性结果/或双重输入。
虽然我发现用linq解决这个问题很有意思,但我个人不会这样做。如果我在9个月后必须返回此代码来维护它,我会更乐意阅读这个:
CurrencyCode
而不是开始记起当时我写的那些da hack以及当我写回来时我可能想到的......
答案 1 :(得分:2)
首先选择匹配的货币代码,然后按所选代码分组。
var groupedRates = rate
.Select(r => new
{
rate = r,
code = currencyCode.FirstOrDefault(c => r.CurrencyCode.Contains(c))
})
.GroupBy(x => x.code, x => x.rate); //maybe you don't want to throw away the resolved code like I do in the element selector...
编辑:我想我有点专注于分组方面。由于您想要包含所有货币代码并提及特定的输出结构,因此不要忘记分组,只需选择结果:
var groupedRatesList = currencyCode
.Select(c => new CurrencyMap
{
Code = c,
currency = rate
.Where(x => x.CurrencyCode.Contains(c))
.Select(x => new Currency
{
BranchName = x.BranchName,
CurrencyCode = x.CurrencyCode, // or maybe you want to insert c here?
CurrencyName = x.CurrencyName
})
.ToList()
})
.ToList();
答案 2 :(得分:1)
不是最好的方法,因为这种方法假定你有货币的固定长度,但你可以试试这个: -
int currencyLength = currencyCode.First().Length;
var result = rate.Where(x => currencyCode.Any(z => x.CurrencyCode.Contains(z)))
.GroupBy(x => x.CurrencyCode.Substring(0, currencyLength))
.Select(x => new
{
Currency = x.Key,
List = x.ToList()
});
答案 3 :(得分:0)
试试这个;
I will move from right to left
<div class="d1">
</div>
I will not move !
<div class="d2">
</div>
I will move from left to right
<div class="d3">
</div>