question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Easier way to use void function in Linq

See original GitHub issue

Below example, I use fun() to wrap WriteLine function. But it’s a little bit redundant … Is any easier way to use void function in Linq ?

public Subsystem<string> Upgrade(string path) =>
    from fileinfo in TryGetFileInfo(path)
    let _ = fun(() => Console.WriteLine($"File {fileinfo.Fullname} size is {fileinfo.length} byte."))()
    from content  in ReadFile(fileinfo)
    select content;

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
louthycommented, Aug 7, 2019

At a computer now, so this will work:

   fun<string>(Console.WriteLine)("Hello");

Unfortunately, you do need to provide the generic parameter.

1reaction
gwinteringcommented, Aug 7, 2019

We can slightly improve the syntax from

let _ = fun(() => Console.WriteLine("info"))()

to

let _ = ignore(() => Console.WriteLine("info"))

with this additional ignore method

public static Unit ignore(Action action)
{
    action();
    return unit;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending items in a LINQ sequence to a method that returns ...
First of all, your current code will not work. Select , and most other LINQ methods, use deferred execution, meaning that they don't ......
Read more >
Linq IEnumerable extensions for void functions
I found myself wanting to use Linq to map void methods to an IEnumerable to modify all items. The existing Linq methods require...
Read more >
How do I return data from a void statement in C# using Linq
You cannot use return in a void method to return any value - becasue declaring a method as void is explicitly saying "this...
Read more >
Write LINQ queries in C# | Microsoft Learn
This example shows how to use method syntax on the results of a query clause. Just enclose the query expression in parentheses, and...
Read more >
7 tricks to simplify your programs with LINQ - Igor Ostrovsky
If so, you are quite wrong! Igor Ostrovsky has put together an excellent post outlining simple, yet effective, ways to use LINQ to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found