• 0

Passing a delegate as a parameter (LINQ) ... C#


Question

Helloooooooo!!!!!! I'll keep this short. I have forgotten how it is called (and its syntax) when you pass a delegate as a parameter for doing the same query. I thought I had bookmarked the page where I saw that... :(

 

In english: 

 

Instead of doing something like this: 

public void SearchMethod(string value, int flag)
{
     if(flag == 1)
     {
          DBContext.Where(x => x.Contains(value));
     }

     else if(flag == 2)
     {
          DBContext.Where(x => x.Contains(value)).GroupBy(x => x.id)
     }
     else if(flag == 3)
    { 
          DBContext.Where(x => x.Contains(value)).GroupBy(x => x.Name)
    }
}

I would pass a Func<> as a paramter and I'll save myself from grabbing all those "if"s

public void SearchMethod(Func<string> query, int flag)
{
     //Do something with the query and save all those if
     // I don't remember the syntax used!
}

5 answers to this question

Recommended Posts

  • 0
  On 14/01/2015 at 14:33, SledgeNZ said:

I'll keep it shorter, look up predicates.

 

: D

Thanks :)Right now I'm not looking for predicates.

 

It's something similar to this answer:

http://codereview.stackexchange.com/a/5506

 

Edit:

Sorry!!!!

Yes, predicates are part of the equation :) I need to know how to do it with Func<> or Expression Trees...

  • 0

I don't understand what you're trying to do, but perhaps something like this (assuming DBContext contains DBItems and you actually want them back) ?

public IEnumerable<DBItem> SearchMethod<T>(string value, Func<DBItem, T> groupByFunc)
{
     var filtered = DBContext.Where(x => x.Contains(value));
     return groupByFunc == null
         ? filtered
         : filtered.GroupBy(groupByFunc);
}
It's not a big improvement, I've just bound the common case to a variable, encoded whether to use groupByFunc using a potentially null parameter and delegated the choice of the GroupBy function to the caller; either way the code does the same thing. But it's not clear what you're looking for.
  • 0

Thanks all! This is what I meant:

  Quote

 

t generally helps if you know the specific type of context that you are targeting.

For instance, let's say you are targeting a context that contains a collection of string objects and you wanted to search them. You would define a function like the following which accepted a function like so :

public void SearchMethod(Func<string,bool> query, int flag)
{
switch(flag)
{
case 1:
// Perform your search
DBContext.YourTable.Where(query);
break;
case 2:
DBContext.YourTable.Where(query).GroupBy(x => x.id);
break;
case 3:
DBContext.YourTable.Where(query).GroupBy(x => x.Name);
break;
}

}

Additionally, you could also pass in a function to handle your GroupBy query as well similar to the approach mentioned in this Stack Overflow response.

 

From:

forums.asp.net/p/2033405/5854959.aspx?p=True&t=635584716606703187

 

Thank you guys!!!! :D :D  Your answers gave me another idea for another part of my project!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Occasionally crazy tyrants occupy palaces using swords.  
    • Switch to another OS if you're into tinfoil hat mentality, or just uninstall the app. Oh, wait, even after uninstalling it, can Microsoft still be looking at everything? We'll never know, so, yeah, that's that. If you use Windows, you need to live with those options.
    • Minecraft Chase the Skies update and Vibrant Visuals graphics overhaul lands next week by Pulasthi Ariyasinghe The second major game update of the year for Minecraft is almost here. Mojang today announced that its next Game Drop, Chase the Skies, is landing on June 17. Alongside it, the highly anticipated graphics overhaul, dubbed Vibrant Visuals, will be available too, making the day a rather big occasion for Minecraft fans. The Chase the Skies update has a large number of gameplay features and content, but one of the biggest is the expansion of the Ghast. Now, players will be able to find Happy Ghasts as a tameable mob, letting up to five players ride them on journeys across the skies. To get one, players will have to find small, dehydrated Ghasts near fossil structures in the Nether, then bring it to the overworld and feed it plenty of water. It is also possible to craft dried Ghasts from Nether materials. Other features of this update include a handy player locator bar, better leads functionality, craftable saddles, a new music disc, and more. As for the Vibrant Visuals upgrade, Mojang is touting a visual refresh that lets players witness how lights and shadows affect the Minecraft world. "Fly high with your happy ghast to watch the sunrise reflect on the rippling ocean, make art with shifting shadows, descend into dark depths, or simply sit back and enjoy the vivid beauty of each biome," says the studio. "With updated in-game visuals you’ll see how light and shadow transform the Overworld – water reflects, forests become shaded, fog drifts, and waterfalls glimmer in the light." The currently supported devices for Vibrant Visuals include Xbox Series X|S, Xbox One, PlayStation 4, PlayStation 5, PC, as well as Android (Android: Adreno 640, Mali-G68, Mali-G77, or Xclipse 530 or higher) and iOS (A12 or M1 or higher.)   Unfortunately, the Vibrant Visuals upgrade will only be available on the Bedrock Edition of Minecraft, at least for now. Mojang has said that it plans to bring the visual overhaul to the Java Edition of Minecraft at a later date, but no information or release window has been announced just yet.
    • Once people get use to the square-ish case, they'll probably switch the display to square as well.
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      560
    2. 2
      ATLien_0
      255
    3. 3
      +Edouard
      163
    4. 4
      +FloatingFatMan
      157
    5. 5
      Michael Scrip
      109
  • Tell a friend

    Love Neowin? Tell a friend!