0

这个问题是寻求技术帮助来投影可枚举以满足指定条件。

说,

一个球包含两个红球和三个蓝球。根据以下条件,我必须选择球。

(i)  Each pair should contain one blue ball and one red ball
(ii) if the ball is "Blue1" ignore it 

预期的结果是

{"Blue2","Red1" }
{"Blue2","Red2" }
{"Blue3","Red1" }
{"Blue3","Red2" }

只是我想完成以下代码来投影结果。

var bag = new[] { "Red1", "Red2", "Blue1", "Blue2", "Blue3" };
var BallsProjection =
                 from blueball in bag
                 from redball in bag
                 **(What is the condition required here to select the balls)**
                 select new { ball1 = blueball, ball2 = redball };
4

1 回答 1

0

您可以使用以下代码来获得您想要的结果。

 var BallsProjection =
               from blueball in bag
               from redball in bag
               where 
               blueball.Contains("Blue") && redball.Contains("Red") && blueball.CompareTo("Blue1") !=0
             select new { ball1 = blueball, ball2 = redball };

您可以增强此代码以满足您的要求。我尚未测试此代码。

于 2013-06-24T10:40:58.730 回答