troubleshooting Question

LINQ Expression to return a string array based on the occurence of a given string

Avatar of johnw322
johnw322 asked on
.NET Programming
3 Comments1 Solution1780 ViewsLast Modified:
I was hoping someone could help me with a LINQ expression that I can't seem to get my arms around. Let's say I have the following method that returns a filtered string array given the name of a fruit and the occurence of where the fruit falls in the original array.

The returned array should contain a group of fruits beginning with the given fruit and its occurence in the original string and "TAKE" until that fruit occurs in the original array again.

In case this sounds confusing, here are some examples of what would be returned from the method below:

Example 1:
GetFruitGroup("Apple", 1);
Returns: { "Apple", "Banana", "Peach" }

Example 2:
GetFruitGroup("Banana", 3);
Returns: { "Banana", "Strawberry" }

Example 3:
GetFruitGroup("Banana", 2);
Returns: { "Banana" }

Example 4:
GetFruitGroup("Peach", 1)
Returns: { "Peach", "Apple", "Orange", "Banana", "Banana", "Strawbery" }
public static string[] GetFruitGroup(string fruitToStartAt, int occurence)
	{
        string[] fruits = new string[] { "Apple",
                                         "Banana",
                                         "Peach",
                                         "Apple",
                                         "Orange",
                                         "Banana",
                                         "Banana",
                                         "Strawberry" };
 
        // var fruitGroup = LINQ Expression
 
        return fruitGroup;
	}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros