0

i have 2 tables like that:

Table1(AdjSapanca)

ItemID| Kategori |
------+----------+
7     | 25       |  
6     | 26       |  
5     | 27       |  
4     | 28       |  
3     | 29       | 
2     | 30       |  
1     | 31       |  

Table2(Kategoris)

ID    | DisplayN |
------+----------+
25     | Testo    |  
26     | Testo1   |  
27     | Testo2   |  
28     | Testo3   |  
29     | Testo4   | 
30     | Testo5   |  
31     | Testo6   |  

What I'm trying to do:

Print DisplayN corresponding to ITEMID to table row.

item.Id

at my code gives me the value of ItemID at AdjSapanca table.

My last point at below:

@if (@Model.Kategoris.Where(p => p.Id == item.Id) != null)
{
<td>
@Model.Kategoris.Where(p => p.Id == Model.KategoriAdjSapanca.Where(x => x.Id == p.Id).FirstOrDefault().Kategori).FirstOrDefault().DisplayN
</td>

and i'm getting that error:

ArgumentNullException: Value cannot be null. (Parameter 'source') System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) System.Linq.Enumerable.Where(IEnumerable source, Func<TSource, bool> predicate) AspNetCore.Views_Home_Index.b__12_10(KategoriT p) in Index.cshtml + @Model.Kategoris.Where(p => p.Id == Model.KategoriAdjSapanca.Where(x => x.Id == p.Id).FirstOrDefault().Kategori).FirstOrDefault().Kategori

Any suggestions? What i am doing wrong?

Eren Temelli
  • 333
  • 1
  • 14
  • The result for `FirstOrDefault()` when no records are matched is probably NULL. To debug, I sugegst you inspect each element of your expression until you work out where the null is coming from. It could be – Nick.Mc Jan 05 '22 at 11:01
  • @Nick.McDermaid `FirstOrDefault()` will return null forsure. If you dont want null you should use `First()` but then, you have to handle exception. – Bartosz Olchowik Jan 05 '22 at 11:02
  • @Nick.McDermaid While using First(), it still returns null. – Eren Temelli Jan 05 '22 at 11:06
  • You mean it returns the same error? You have a bunch of chained expressions there, any single one of them could be causing the issue. The first step in troubleshooting is breaking into pieces. I suggest you use the debugger to inspect each of the pieces of your chained expression. What is `Model.KategoriAdjSapanca.Where(x => x.Id == p.Id).FirstOrDefault()`? What is `Model.KategoriAdjSapanca.Where(x => x.Id == p.Id).FirstOrDefault().Kategori`? – Nick.Mc Jan 05 '22 at 11:49

0 Answers0