• 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

    • I think you answered why it gets so much attention in your comment. It's designed to make Windows accessible to everyone! It's a core component of the Windows UI, and 30 years of its existence is why it remains such a key component and receives so much attention to this day!
    • I agree with your points, particularly around workflows. But I think this is a symptom of the way Microsoft plans, builds, and releases its OS's, not about its UI ideals itself. It has historically been large jumps in UI changes between releases, XP > Vista > 7 > 8 were ALL significant changes in how we performed tasks. Then Win10 was released and updated twice a year with minor changes. This was somewhat nice as the changes were often minimal, however as it looked like the same OS for the most part, when something did change (like the start menu), it was a bit confusing for people who didn't understand that they had actually been upgraded, and why just this one key part of their workflow had changed. But I believe that was too slow, and didn't allow their future designs to be played out in that release structure. So the "last Windows version ever" was superseded with a new UI and overhauled once more. It too has changed quite a bit in its 4 years, but I think there needs to be an understanding of that change, which I guess we have in the "24H2" version numbering, but not in the marketing that most users know. The most consistent and clear communication of changes I think is done with MacOS. While I'm not a fan of Apples software, their clear and consistent release schedule, and progressively planned changes to the OS is easy to understand, leading towards a well thought out goal. Their users understand the branding of the big updates, and that changes will occur when they update, yet these are more subtle than changes we see in Windows 7 > 8 > 10 > 11 releases. On that, Windows 10/11 version updates just get lost in the vast collection of update types in Windows Update, and as such most users wouldn't know if the were on 22H2, 23H2 or 24H2, or even know what those meant. My thoughts are that Microsoft will drop the Win11/Win12 branding once Windows 10 has finally died. And hopefully, make it a bit more consistent with feature/UI updates sticking to an improved branded version/year release rather than "moments" or just randomly enabled elements in a random monthly update. Until then, even us tech users rarely know what features are in what release and if we will see it on our own computers. Get with it MS!
    • Hello, Horizon Data Systems, the developer, has been around for 27 years.  I do not recall hearing anything bad about them, but I also haven't used their software, either. Regards, Aryeh Goretsky  
    • Hello, I am wondering if this will go through an appeal process, or if the ruling is final? Regards, Aryeh Goretsky
    • Hello, I think it also depends upon what sorts of software you develop. My employer makes software that does rely on operating system version-specific functionality, and that is typical and normal behavior for our industry. While we do provide support for older versions of operating systems, it is only for a certain amount of time. If you still want to use our software after that, it becomes a custom support package, which isn't cheap. Regards, Aryeh Goretsky
  • Recent Achievements

    • Reacting Well
      pelaird earned a badge
      Reacting Well
    • Mentor
      The Werewolf went up a rank
      Mentor
    • First Post
      Myriachan earned a badge
      First Post
    • Week One Done
      DrRonSr earned a badge
      Week One Done
    • Week One Done
      Sharon dixon earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      603
    2. 2
      ATLien_0
      214
    3. 3
      +FloatingFatMan
      169
    4. 4
      Michael Scrip
      151
    5. 5
      Som
      151
  • Tell a friend

    Love Neowin? Tell a friend!