• 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

    • From what I understand, RAID 1 makes a copy of one disk. How does it do this? If something happened to one drive, could I take the other one out, plug it into a PC and use it like a normal disk? I have two hard drives that have the same information on - a bit of protection if one drive fails. But I've just been manually copying the data twice.
    • Microsoft SharePoint gets Modern Page Templates to speed up page creation by Paul Hill SharePoint, Microsoft’s enterprise content management solution that allows organizations to set up internal pages and share documents, has just got a big update with the new Modern Page Templates. Microsoft said that it’s going to be rolling out the feature to customers globally between early July and early August. Organization members that want to set up pages on SharePoint using the new templates can do so from multiple entry points including the Site Home (the home of a Communication or Teams site), App Bar (the persistent navigation pane on the left of SharePoint), and the New Web Part (a webpart that displays and creates news posts directly on pages). By giving colleagues multiple access points to the new templates, there’s more chance they’ll be found and used. By including plenty of swanky templates, Microsoft is making it so you can spend less time designing pages, and more time filling them with content that really matters. If you want to create a new page using the templates, just go to New > Page and select a template “From Microsoft”. If you were making a news post, you would go to New > News post then you’d be presented with the new Template Gallery where you can select a custom template created by your colleagues under “Saved on this site”. While these new templates are coming to customers globally next month, Microsoft is already busy working on new features to make templates easier to discover and make them more useful. For example, it is building tenant-wide custom templates that can be published across sites for organization-wide use, and it’s working on Copilot templates that you can use when creating pages with Copilot. Neither of these features was given a release timeline. Customers won’t need to do anything to start using Modern Page Templates as they’ll be available automatically as it rolls out worldwide across tenants. For those who want to dive deeper, Microsoft will be publishing additional documentation and guidance about this feature soon.
    • Times are changing: https://arstechnica.com/gaming...ndows-11-ars-testing-finds/
    • Unless there is “bug” that all of a sudden sends your messages to Meta. Where have I heard this before?!
  • Recent Achievements

    • One Month Later
      Leonard grant earned a badge
      One Month Later
    • Week One Done
      pcdoctorsnet earned a badge
      Week One Done
    • Rising Star
      Phillip0web went up a rank
      Rising Star
    • One Month Later
      Epaminombas earned a badge
      One Month Later
    • One Year In
      Bert Fershner earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      537
    2. 2
      ATLien_0
      205
    3. 3
      +FloatingFatMan
      167
    4. 4
      Michael Scrip
      151
    5. 5
      Som
      126
  • Tell a friend

    Love Neowin? Tell a friend!