has some doubts with LINQ queries on objects ...
I have descriptions of filters that I want to keep confidential, and at the given breakpoints in the code I set some flags active. At the end of processing, I want to filter out the active flags.
If any flags are active, I want to write them to the console. (I want all the marked control points to be on the same line, so I can possibly change the code to later throw an exception).
This is the code:
using System; using System.Collections.Generic; using System.Linq; using System.Xml; namespace checkpoints { public class Program { public static void Main(string[] args) { var filters = new Filters().FilterList; filters[0].flag = true; filters[2].flag = true; var query = filters.Where(f => f.flag).Select(f => f.desc); Console.WriteLine("Filter points active: "); string fpoints = System.String.Empty; foreach(string fp in query) { fpoints = fpoints + fp + System.Environment.NewLine; } Console.WriteLine(fpoints); } } public class Filters { public List<Filter> FilterList = new List<Filter>{}; public Filters() { foreach(var def in Filters.def_desc) { this.FilterList.Add(new Filter(false, def)); } } private readonly static string[] def_desc = new string[3] { "Filter AX2123: Failed file write.", "Filter XVB231: Failed table load.", "Filter FZD358: Transaction halted." }; public class Filter { public bool flag; public readonly string desc; public Filter(bool flag, string desc) { this.flag = flag; this.desc = desc; } } } }
It works. My problem is the need to run LINQ for each of the results of the query (which I assume is an IEnumerable<Filter> ) to extract the rows. If I have already fulfilled the request (i.e. all the code has passed), why do I need to "run again"? It looks like it weighs terribly and not very elegant ... Any thoughts?
source share