Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简化以下算法_C#_Linq_Dictionary - Fatal编程技术网

C# 简化以下算法

C# 简化以下算法,c#,linq,dictionary,C#,Linq,Dictionary,我使用LINQ查询来检索存储在字典中的一定数量的KeyValuePair(名为Prix,可以包含1到n个KeyValuePair)。使用此查询的结果,我填写一个DataRow(在开关中)。存储在DataRow中的价格的最大值为3,除非hasEAN为真,在这种情况下,最大值为2。如果我在大奖赛中只有一个价格,我只存储一个价格。算法如下: List<KeyValuePair<string, string>> prices; if (this.Prix.Count > 1

我使用LINQ查询来检索存储在字典中的一定数量的
KeyValuePair
(名为Prix,可以包含1到n个
KeyValuePair
)。使用此查询的结果,我填写一个
DataRow
(在开关中)。存储在
DataRow
中的价格的最大值为3,除非
hasEAN
为真,在这种情况下,最大值为2。如果我在大奖赛中只有一个价格,我只存储一个价格。算法如下:

List<KeyValuePair<string, string>> prices;
if (this.Prix.Count > 1)
{
    if (this.Prix.Count >= 2)
    {
        if (this.hasEAN)
        {
            prices = (from kvp in this.Prix
                      select kvp).Take(2).ToList();
        }
        else
        {
            prices = (from kvp in this.Prix
                      select kvp).Take(3).ToList();
        }
    }
    else
    {
        prices = (from kvp in this.Prix
                  select kvp).Take(1).ToList();
    }
}
else
{
    prices = null;
}

switch (prices.Count)
{
    case 1:
        res[8] = prices[0].Key;
        res[9] = prices[0].Value;
        break;
    case 2:
        res[8] = prices[0].Key;
        res[9] = prices[0].Value;
        res[10] = prices[1].Key;
        res[11] = prices[1].Value;
        break;
    case 3:
        res[8] = prices[0].Key;
        res[9] = prices[0].Value;
        res[10] = prices[1].Key;
        res[11] = prices[1].Value;
        res[12] = prices[2].Key;
        res[13] = prices[2].Value;
        break;
    default:
        break;
}
价目表;
如果(this.Prix.Count>1)
{
如果(this.Prix.Count>=2)
{
if(this.hasEAN)
{
价格=(来自本.Prix中的kvp)
选择kvp.Take(2.ToList();
}
其他的
{
价格=(来自本.Prix中的kvp)
选择kvp.Take(3.ToList();
}
}
其他的
{
价格=(来自本.Prix中的kvp)
选择kvp.Take(1.ToList();
}
}
其他的
{
价格=零;
}
开关(价格.计数)
{
案例1:
res[8]=价格[0]。键;
res[9]=价格[0]。价值;
打破
案例2:
res[8]=价格[0]。键;
res[9]=价格[0]。价值;
res[10]=价格[1]。键;
res[11]=价格[1]。价值;
打破
案例3:
res[8]=价格[0]。键;
res[9]=价格[0]。价值;
res[10]=价格[1]。键;
res[11]=价格[1]。价值;
res[12]=价格[2]。键;
res[13]=价格[2]。价值;
打破
违约:
打破
}

我觉得可以简单得多,但我不熟悉LINQ查询。

List prices=this.Prix.Count
List prices=this.Prix.Count至少可以缩短代码的第一部分:

List<KeyValuePair<string, string>> prices = this.Prix.Count <= 1 ? null : (from kvp in this.Prix select kvp).Take(this.Prix.Count < 2 ? 1 : this.hasEAN ? 2 : 3).ToList();

switch (prices.Count)
{
    case 3:
        res[12] = prices[2].Key;
        res[13] = prices[2].Value;
            goto case 2;
    case 2:
        res[10] = prices[1].Key;
        res[11] = prices[1].Value;
            goto case 1;
    case 1:
        res[8] = prices[0].Key;
        res[9] = prices[0].Value;
        break;
    default:
        break;
}
List<KeyValuePair<string, string>> prices = null;
if (Prix.Any())
{
    int count = Prix.Count == 1 ? 1 : hasEAN ? 2 : 3;
    prices = Prix.Take(count).ToList();
}
价目表=null;
if(Prix.Any())
{
int count=Prix.count==1?1:hasEAN?2:3;
价格=Prix.Take(count.ToList();
}

至少可以缩短代码的第一部分:

List<KeyValuePair<string, string>> prices = null;
if (Prix.Any())
{
    int count = Prix.Count == 1 ? 1 : hasEAN ? 2 : 3;
    prices = Prix.Take(count).ToList();
}
价目表=null;
if(Prix.Any())
{
int count=Prix.count==1?1:hasEAN?2:3;
价格=Prix.Take(count.ToList();
}

从其他答案中可以看出,这在Linq的一行中当然是可能的,但请考虑一下未来的可维护性,如果您的规则发生变化,引入更多的复杂性,您将来会怎么做? 您可以简化为只定义一次查询:

List<KeyValuePair<string, string>> prices;
//we'll always want at least one
int numberToTake=1;
//if we have more than one price though
if (this.Prix.Count>1)
{
  //check if we have more than 2
   if (this.Prix.Count >= 2)
   {
   //and if we have an EAN, take only 2 prices
    if (this.hasEAN)
    {
        numberToTake=2;
    }
    else
    {
     //if no EAN, take 3 prices.
      numberToTake=3;
    }
  }
}
prices = (from kvp in this.Prix select kvp).Take(numberToTake).ToList();
switch (prices.Count)
{
 //...your switch here.
}
价目表;
//我们总是至少要一个
int numberToTake=1;
//如果我们有一个以上的价格
如果(本次大奖赛计数>1)
{
//检查是否有超过2个
如果(this.Prix.Count>=2)
{
//如果我们有一个EAN,只需要2个价格
if(this.hasEAN)
{
numberToTake=2;
}
其他的
{
//如果没有EAN,则取3个价格。
numberToTake=3;
}
}
}
prices=(在此.Prix中从kvp选择kvp).Take(numberToTake.ToList();
开关(价格.计数)
{
//…你的开关在这里。
}

为了获得最佳结果,请将其滚动到业务规则中的一个函数中,用于返回
numberToTake
的对象(无论
this
是什么),但如果您只是因为它看起来更好而只想压缩到一行,我敦促您重新考虑。

从其他答案中可以看出,这在Linq的一行中当然是可能的,但请考虑一下未来的可维护性,如果您的规则发生变化,引入更多的复杂性,您将来会怎么做? 您可以简化为只定义一次查询:

List<KeyValuePair<string, string>> prices;
//we'll always want at least one
int numberToTake=1;
//if we have more than one price though
if (this.Prix.Count>1)
{
  //check if we have more than 2
   if (this.Prix.Count >= 2)
   {
   //and if we have an EAN, take only 2 prices
    if (this.hasEAN)
    {
        numberToTake=2;
    }
    else
    {
     //if no EAN, take 3 prices.
      numberToTake=3;
    }
  }
}
prices = (from kvp in this.Prix select kvp).Take(numberToTake).ToList();
switch (prices.Count)
{
 //...your switch here.
}
价目表;
//我们总是至少要一个
int numberToTake=1;
//如果我们有一个以上的价格
如果(本次大奖赛计数>1)
{
//检查是否有超过2个
如果(this.Prix.Count>=2)
{
//如果我们有一个EAN,只需要2个价格
if(this.hasEAN)
{
numberToTake=2;
}
其他的
{
//如果没有EAN,则取3个价格。
numberToTake=3;
}
}
}
prices=(在此.Prix中从kvp选择kvp).Take(numberToTake.ToList();
开关(价格.计数)
{
//…你的开关在这里。
}

为了获得最佳效果,请将其应用到业务规则中返回
numberToTake
的对象(无论
this
是什么)的函数中,但如果您只是因为外观更好而只想压缩到一行,我会敦促您重新考虑。

您可以将切换情况简化为:
(int i=0;i
if(this.Prix.Count>1)和if(this.Prix.Count>=2)中的工作代码都是相同的测试。我想你只需要三个案例就可以了。Prix.Count:0,1,>1,你可以用if/if-else/else块来做。@SriramSakthivel谢谢,我不熟悉所有的stackexchange网络。我以后会在那里发布。你可以将你的切换案例简化为:
for(int I=0;I
if(this.Prix.Count>1)和if(this.Prix.Count>=2)中的工作代码都是相同的测试。我想你只需要三个case.Prix.Count:0,1,>1,你可以用if/if-else/else块来做。@SriramSakthivel谢谢,我不熟悉所有的stackexchange网络。我以后会在那里发布。C不允许case语句失效,
中断
(或类似)是强制性的。我不知道
转到case
语句!谢谢。C#不允许case语句失败,
中断
(或类似)是强制性的。我不知道
goto case
语句!谢谢。我总是忘记条件运算符。我对编程相当陌生,而且我经常滥用if语句。非常感谢,这真的很容易读写。我总是忘记条件运算符。我对编程相当陌生ing和我经常滥用if语句。非常感谢,这真的很容易阅读和书写。我的业务规则永远不会改变。如果我必须改变它,我会记住你的建议。谢谢:)我的业务规则是