find a location for property in a new city

Tuesday, 27 March 2012

Reuse Select function in Linq

You can reuse Linq Select functions by defining a delegate Func field that can be used in multiple Linq queries.

I had a few Linq to Objects queries that were querying the same collection but had different Where clauses that were nested between various if statements. The similarity between all of these statements was the Select function. This was quite long itself and it was annoying seeing such repetition. DRY!

So I tried to create a Func parameter that I could just pass into my Select statement which previously, for example could look like this:

ReturningPerson person;
if (iFeelLikeIt)
{
    person = people.Where(person => person.IsNameFunny)
                .Select(person =>
                    new ReturningPerson
                    {
                        DateOfBirth = input.DateBorn,
                        Name = string.Format("{0} {1}",
                            input.FirstName, input.LastName),
                        AwesomenessLevel = input.KnowsLinq ? "High" : "Must try harder",
                        CanFlyJet = false
                    })
                .FirstOrDefault();
}
else
{
    person = people.Where(person => person.Toes == 10)
                .Select(person =>
                    new ReturningPerson
                    {
                        DateOfBirth = input.DateBorn,
                        Name = string.Format("{0} {1}",
                            input.FirstName, input.LastName),
                        AwesomenessLevel = input.KnowsLinq ? "High" : "Must try harder",
                        CanFlyJet = false
                    })
                .FirstOrDefault();
}

This could be modified to work like this:

Expression<Func<InputPerson, ReturningPerson>> selector = (input) =>
    new ReturningPerson
    {
        DateOfBirth = input.DateBorn,
        Name = string.Format("{0} {1}",
            input.FirstName, input.LastName),
        AwesomenessLevel = input.KnowsLinq ? "High" : "Must try harder",
        CanFlyJet = false
    };
ReturningPerson person;
if (iFeelLikeIt)
{
    person = people.Where(person => person.IsNameFunny)
                .Select(selector)
                .FirstOrDefault();
}
else
{
    person = people.Where(person => person.Toes == 10)
                .Select(selector)
                .FirstOrDefault();
}

Follow britishdev on Twitter

22 comments:

  1. Thank you very much for sharing this information with us!
    professional essay writers

    ReplyDelete
  2. Great blog! It is very useful information! It is really nice.

    ReplyDelete
  3. The academic burden is increasingly gettig heavier among many students, especially those who are also working, as people are being exposed to many issues concerning their areas of study. For instance, a student who is pursing a degree in civil engineering will be required to have adequate knowledge on various issues in the environment!Ok, it is cool paper writing services save your time!

    ReplyDelete
  4. Nice article,short and informative as I like))If you someday looking for essay advanced-writer.com please welcome to my website.

    ReplyDelete
  5. Thanks for this post.Academic paper writing requires in-depth research and analytical skills and exceptional writing service that combines all of these ingredients.I recommend you this www.essays-shark.net one

    ReplyDelete
  6. Interesting site! So much feedback, it's nice to see! I think many will agree with me that now there are many different sites! I would to recommend http://www.essay-mania.com - it's a good project, where everyone can find what he needs!

    ReplyDelete
  7. Oh, no. These tables and charts are too complicated for me. That is why I always order papers in Maths at Custom-Essays-Online.com.

    ReplyDelete
  8. For me it looks like some curse.. I didn't understand even a word(( but I like your blog's design) very cute. I have mine too, but it is more cheerful and sweet. I like to post recipes and vintage photos. Sometimes I order articles for my blog on http://papers-writings.com

    ReplyDelete
  9. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. . Assignment Help

    ReplyDelete
  10. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting.
    Assignment Help

    ReplyDelete
  11. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. . buy sunglasses

    ReplyDelete
  12. This is wonderful blog and amazing thanks for sharing. Buy assignment uk

    ReplyDelete
  13. I find the information very helpful for all students. You do nice job. Keep on writing and use this reliable source http://academic-writings.com if you need high-quality and original academic writings from degree-holding writers.

    ReplyDelete
  14. This blog is really informative among all thanks a lot for sharing. Economics Assignment Help

    ReplyDelete
  15. Overcoming the Shutdown Crisis: Helping Dysfunctional Politicians With Family Psychology paper writing awesome site

    ReplyDelete
  16. If you have difficulties with your essay, i think best solution for you in this situation visit this site http://essay-professors.com and order your assignments here!

    ReplyDelete
  17. Try our best academic research writing service http://www.essay-experts.org/

    ReplyDelete
  18. AmazingEmulator is the software which is used to run the Android apps on Windows OS. Mobdro Download system employing ARC Welder or Bluestacks so you can stream no cost videos immediately from your computer system. nice.

    ReplyDelete