c# - Extracting lambda expression from LINQ -
i have next chunk of code
var query = wordcollection.select((word) => { return word.toupper(); }) .where((word) => { return string.isnullorempty(word); }) .tolist();
suppose want refactor code , extract lambda expression clause. in visual studio select lambda , refactor -> extract method. doing have linq modified to
var query = wordcollection.select((word) => { return word.toupper(); }) .where(newmethod1()) .tolist();
and newmethod1() declared as
private static func<string, bool> newmethod1() { return (word) => { return string.isnullorempty(word); }; }
the question why new method not have input parameters, delegate func states newmethod1() should have string input parameter?
to expected result, mark part string.isnullorempty(word)
, extract method:
private bool newmethod(string word) { return string.isnullorempty(word); }
what got because extract created method returns delegate. not method matches delegate. method returns method. latter accepts string parameter word
, returns bool result.
sure doing above changes code to:
.where((word) => newmethod(word))
but can safely change to:
.where(newmethod)
side note:
no need use return
keyword in linq queries or one-line lambda, can refactor query this:
var query = wordcollection.select(word => word.toupper()) .where(word => string.isnullorempty(word)) .tolist();
Comments
Post a Comment