Sep 25 2015

How can I use Linq and c# to compare a property against a List<string>

I have code that is filtering a List using code like this..

    List<Product> products = new List<Product>() { /*<INIT THE COLLECTION>*/ }
    //get the ones you need.
    var newListOfProducts = products.Where(p=>p.MyProperty == "prop1" || p.MyProperty == "prop2" || p.MyProperty == "prop3");

My preferences is to use syntax like this...

List<string> stringsToCompare = new List<string>() {"prop1","prop2","prop3"};
var newListOfProducts = products.Where(p=>p.MyProperty.IsInList(stringsToCompare));

This way I can dynamically build the stringToCompare instead of hard-coding them.

But I can't figure out how to do it despite googling for the last half-hour. I think Intersect or Union can work but I can't get the syntax right.

Sep 25 2015

How can I use Linq and c# to compare a property against a List<string>

I have code that is filtering a List using code like this..

    List<Product> products = new List<Product>() { /*<INIT THE COLLECTION>*/ }
    //get the ones you need.
    var newListOfProducts = products.Where(p=>p.MyProperty == "prop1" || p.MyProperty == "prop2" || p.MyProperty == "prop3");

My preferences is to use syntax like this...

List<string> stringsToCompare = new List<string>() {"prop1","prop2","prop3"};
var newListOfProducts = products.Where(p=>p.MyProperty.IsInList(stringsToCompare));

This way I can dynamically build the stringToCompare instead of hard-coding them.

But I can't figure out how to do it despite googling for the last half-hour. I think Intersect or Union can work but I can't get the syntax right.

Sep 15 2015

Answer by Seth Spearman for Scheduled console app vs Windows service? When is it appropriate to use each

Having had to do write, debug, deploy and support both, I prefer Scheduled Tasks far and away. I don't doubt that there are use cases where this would be the wrong choice. But for any relatively trivial task a console app running as a schedule task works great.

In my experience, scheduled tasks are extremely reliable. I don't recall a single failure and I support about a half dozen different scheduled tasks that run anywhere from daily to every 15 minutes. (Not that there have been no failures but they were all code or config issues. And were logged and notifications were sent. The problem has NEVER been with the Task Scheduling infrastucture.)

Task Scheduler tasks can run under any user context and does not need anyone to be logged in.

My biggest plus is in deployment. For console apps, just publish the EXE to the correct target folder. For windows services you need to stop the service (using net.exe), uninstall the service (using InstallUtil.exe), wait (I have the deployment sleep for 25 seconds), publish the EXE and then do it all in reverse (install, start).

If I were to develop a windows service again I would write it as a console app and then find a way to wrap it into a service to make debugging less of a headache.

Sep 15 2015

Answer by Seth Spearman for Scheduled console app vs Windows service? When is it appropriate to use each

Having had to do write, debug, deploy and support both, I prefer Scheduled Tasks far and away. I don't doubt that there are use cases where this would be the wrong choice. But for any relatively trivial task a console app running as a schedule task works great.

In my experience, scheduled tasks are extremely reliable. I don't recall a single failure and I support about a half dozen different scheduled tasks that run anywhere from daily to every 15 minutes. (Not that there have been no failures but they were all code or config issues. And were logged and notifications were sent. The problem has NEVER been with the Task Scheduling infrastucture.)

Task Scheduler tasks can run under any user context and does not need anyone to be logged in.

My biggest plus is in deployment. For console apps, just publish the EXE to the correct target folder. For windows services you need to stop the service (using net.exe), uninstall the service (using InstallUtil.exe), wait (I have the deployment sleep for 25 seconds), publish the EXE and then do it all in reverse (install, start).

If I were to develop a windows service again I would write it as a console app and then find a way to wrap it into a service to make debugging less of a headache.