c# - Anonymous types LinQ - Check for null value - Exception -


I have gone for a while and tried a variety of combinations for my problem.

I first ran into my Anonymous Type I "loop" to fill your Gridwu with results goes well, because there are some real values ​​in all the variables.

But throws the second row to fill the line number 2: Object references are not set to any instance of the object.

This is because I probably specify a bacon object to clear and Gridview can not handle it. I have tried to prepare some solutions but nothing is working yet.

Is there a secret path to understand Linq correctly?

This is my code right now:

  var filteredlist = new information from // external list from information in the list {question = info.QuestionText ?? "Test", correct answer = info.Correct Ansver.OptionText ?? "Test", WrongAnswer1 = info.WrongAnswer1.OptionText ?? "Test", WrongAnswer2 = info.WrongAnswer2.OptionText ?? "Test", // throws error WrongAnswer3 = info.WrongAnswer3.OptionText ?? "Test" // throws error}; GridView1.DataSource = Filtered List; GridView1.DataBind ();   

This tries to do my job.

  var filteredlist = info in list // list // External selection from the list New {Question = Info QuestionText == empty? "Test": info.QuestionText, CorrectAnswer = info.Correct Ansver.OptionText == empty? "Test": info.Correct Ansver.OptionText, WrongAnswer1 = info.WrongAnswer1.OptionText == empty? "Test": info.WrongAnswer1.OptionText, WrongAnswer2 = info.WrongAnswer2.OptionText == empty? "Test": info.WrongAnswer2.OptionText, // throws error incorrect answers3 = info.WrongAnswer3.OptionText == empty? "Test": info.WrongAnswer3.OptionText, // throws error}; GridView1.DataSource = Filtered List; GridView1.DataBind ();   

Try second

  var filteredlist = info from list // External selection from list new {question = information. Q & A text! = Null? "Test": info. Question text, correct answer = information right answer. "Test": info.Correct Ansver.OptionText, WrongAnswer1 = info.WrongAnswer1.OptionText! = Null? "Test": info.WrongAnswer1.OptionText, WrongAnswer2 = info.WrongAnswer2.OptionText! = Null? "Test": info.WrongAnswer2.OptionText, // throws error incorrect Answers3 = info.WrongAnswer3.OptionText! = Null? "Test": info.WrongAnswer3.OptionText, // throws error}; GridView1.DataSource = Filtered List; GridView1.DataBind ();    

Try to check whether WrongAnswer s not clear Is, like:

  Where information from the information in the list! = Select the new {Question = info.QuestionText ?? "Test", correct answer = info.Correct Ansver.OptionText ?? "Test", WrongAnswer1 = info.WrongAnswer1! = Null? Info.WrongAnswer1.OptionText: "Test", WrongAnswer2 = info.WrongAnswer2! = Null? Info.WrongAnswer2.OptionText: "Test", WrongAnswer3 = info.WrongAnswer3! = Null? Info.WrongAnswer3.OptionText: "Test"};   

I prefer to use generic methods for reading abilities, such as:

  public string default Ifnl & lt; TVLuT (TVView price, Funk & lt; TV, string & gt; selector) where TValue: class {if (value! = Null) back selector (value); Return "Test"; // You can also create Reset Generic}   

Now you can:

  function & lt; Answer, string & gt; OptionTextSelector = Answer = & gt; Answer.OptionText; Information from the information in the list! = Select the new {Question = info.QuestionText ?? "Test", Shiansr = default Ifnl (Note. The correct answer, options reader), Gltansser 1 = default Ifnl (Notice. Virsanajr 1, Opshnstekst selector), Gltansevr 2 = default Ifnl (Info. WrongAnswer2, Opshntekstselector), Ricongansevr 3 = default Iphone (Info. WrongAnswer3, OptiontextSelector);};   

The main idea of ​​using the functional approach their solution to very do not obscure the complex to make and do not

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -