What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 19617 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > LINQ Interview Questions > How to assign a computed value to the sa ...

How to assign a computed value to the same array back?

Interview question and answer by: Niladri.Biswas | Posted on: 7/13/2012 | Category: LINQ Interview questions | Views: 382 | | Points: 40


Answer:

Consider the below input

var input = new string[] { "Apple", "Banana", "Mango", "Apple", "Orange", "Mango", "Strawberry", "Apple" };


The problem is to write a query using LINQ that will count the number of fruits and will assign back the value to the same array i.e. we should not create another array for storing the computed values.

Solution


var input = new string[] { "Apple", "Banana", "Mango", "Apple", "Orange", "Mango", "Strawberry", "Apple" };

input = (from a in input
group a by a into g
where g.Count() >= 2
select g.Key + " ( " + g.Count() + " )").ToArray();


Output
Apple ( 3 )

Mango ( 2 )

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Niladri.Biswas

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 5:38:15 AM