0

I have two conditions to the following linq, IsThere and Conditional are provided by the user. They can be true, false, or "not given".

var result = (from DataRow dt in resultForfaultStat.Rows
              select new fault_StatisticsViewModel{
                Name = dt["name"].ToString(),
                SureName = float.Parse(dt["fname"].ToString()),
                IsThere = Boolean.Parse(dt["is_there"].ToString()),
                Conditional = Boolean.Parse(dt["good_or_bad"].ToString())
              })
              .Where(s => s.IsThere == true && s.Conditional == true)
              .ToList();

How should I manage to pass these all dynamically?

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
moris62
  • 983
  • 1
  • 14
  • 41
  • 1
    Can you try to explain me your attempt? – Marco Salerno Aug 22 '18 at 08:34
  • @MarcoSalerno mine is very basic var finalresult= result.Where(s => s.Conditional== x && s.IsThere== y) i get x,y from user,which is true or false – moris62 Aug 22 '18 at 08:37
  • @mortezasol your solution looks pretty fine for me – rs232 Aug 22 '18 at 08:40
  • `.Where(s => s.IsThere == true && s.Conditional == true)` in this line if you replace those with variable? There is no null value for bool so you can't have none of them given .. Perhaps `bool?`? – Drag and Drop Aug 22 '18 at 08:43
  • 1
    In addition to the duplicate: assuming that value is optional **for simple cases** you can also rewrite it as: `Where(x => (!isThereValue.HasValue && x.IsThere == isThereValue.Value) && ...)` where `isThereValue` is `bool?` (with `null` to indicate that comparison isn't required) – Adriano Repetti Aug 22 '18 at 08:44
  • @AdrianoRepett you are right,if i get string from user i cant compare it with bool,if i get bool,what should i do with null?!the solution is your last suggestion? – moris62 Aug 22 '18 at 08:58
  • @DragandDrop your solution ? – moris62 Aug 22 '18 at 08:59
  • @mortezasol if user enters a string then you just need to parse it, something like `bool? TryParseUserBoolean(string text) { if (Boolean.TryParse(text, out bool value) { return value; } return null; }` – Adriano Repetti Aug 22 '18 at 09:07
  • AdrianoRepett comment and mine are pointing in the exact same direction. You need a `bool?`. As you comment about getting the value as string you can simply `if (!string.IsNullOrEmpty(userString_IsThere)&& bool.TryParse(userString_IsThere, out bool variableIsThere)){ result = result.where( s => s.IsThere == variableIsThere); }` – Drag and Drop Aug 22 '18 at 09:08
  • @mortezasol, as you were confuse by your issue, your question was really confusing. You should read the guide line [ask]. Here you gave the code too soon, and didn't spend enought time to explain what you were trying, and the context you were working. Where you could be getting those variable from, their type, their values were important information. The clearer you are the easier it get. Most of the time following this guideline you will find your answer simply by redacting a clear question. without the need to post the question. – Drag and Drop Aug 22 '18 at 09:26
  • 1
    @Heinzi : IMO, Answer for this question won't apply to the duplicate target. Even if both have really close title, the second part of the question clarify the fact that op has issue handeling bool user input (user enter yes , no or nothing). While the ohter question don't have that part and will make adding a answer about parsing an user input really weird. – Drag and Drop Aug 22 '18 at 11:39
  • 1
    @DragandDrop: As far as I can see, the OP has two issues: (1) How to parse yes/no/unknown user input into a suitable data structure and (2) how to use conditional where clauses in LINQ. (1) has only been revealed in the comments (after a lot of back-and-forth), so I still think that the dupe target is appropriate (for (2)). I also don't think that having (1)+(2) together makes a good SO question likely to help other users. – Heinzi Aug 22 '18 at 11:54
  • i also found this question useful and not duplicated –  Aug 22 '18 at 13:05

0 Answers0