Query to find a single object from a single row

simsenVejle

Well-known member
Joined
Feb 14, 2021
Messages
46
Programming Experience
Beginner
Hi,
I have problems to find the correct syntax to get a single result of a single row.

I get the row (categories) and I can see in this row there is a CategoryIsObsolete field (it is that value I want)

But when making af FirstOrDefault query the Category result is null - and I do not know why.



C#:
 DalCategory dalCategory = new DalCategory();
                var categories = dalCategory.GetCategories();

                //This one gives me one row
                categories = categories.Where(x => x.CategoryId == SelectedCategory.CategoryId).ToList();

                //This one give me a null result but there is a CategoryIsObsolete field in the categories??????
                Category result = categories.FirstOrDefault(s => s.CategoryIsObsolete);
                bool oldIsObsolete = result.CategoryIsObsolete;

                if (oldIsObsolete != SelectedCategory.CategoryIsObsolete)
                {
                    //Inaktiv
                    if (oldIsObsolete)
                    {
                        Categories_GetInactive.Remove(SelectedCategory);
                        Categories_GetActive.Add(SelectedCategory);
                    }
                    //Aktiv
                    else
                    {
                        Categories_GetActive.Remove(SelectedCategory);
                        Categories_GetInactive.Add(SelectedCategory);
                    }
                }

Maybe I'm doing to much - but I have first to get the row - and then I want the value for a entry in that row.

Best regards
Simsen :)
 
Solution
Yes I know for sure, there is only one CategoryId - it is the primary key for Category :)
In that case, the code you claimed is the solution is actually not. As suggested, that will actually return an enumerable list, not a single value. FirstOrDefault is indeed the correct option, but you need to use it properly. If you want the CategoryIsObsolete value from the row with a specific CategoryId value then that's what you need to write code to get:
C#:
var name = categories.FirstOtDefault(i => i.CategoryId == 0)?.CategoryIsObsolete;
Note that I have used ?. rather than just . for the final property access. That's because the whole point of FirstOtDefault is that it might return...
I finally find it out by myself


C#:
var name = categories.Where(i => i.CategoryId == 0)
                                .Select(row => row.CategoryIsObsolete);
 
But what you are doing there is not returning a single object of a single row as you requested. You are getting an IEnumerable of all the categories which have a CategoryId == 0. Are you just going on the assumption that CategoryId is a unique value and there will only be a single row that will satisfy that condition?
 
But what you are doing there is not returning a single object of a single row as you requested. You are getting an IEnumerable of all the categories which have a CategoryId == 0. Are you just going on the assumption that CategoryId is a unique value and there will only be a single row that will satisfy that condition?
Yes I know for sure, there is only one CategoryId - it is the primary key for Category :)
 
A DAL (Data Access Layer) is supposed to save to persistence, and load from persistence. Your domain objects should not have IDs.

 
Yes I know for sure, there is only one CategoryId - it is the primary key for Category :)
In that case, the code you claimed is the solution is actually not. As suggested, that will actually return an enumerable list, not a single value. FirstOrDefault is indeed the correct option, but you need to use it properly. If you want the CategoryIsObsolete value from the row with a specific CategoryId value then that's what you need to write code to get:
C#:
var name = categories.FirstOtDefault(i => i.CategoryId == 0)?.CategoryIsObsolete;
Note that I have used ?. rather than just . for the final property access. That's because the whole point of FirstOtDefault is that it might return null, so you have to allow for that. If it does then .CategoryIsObsolete would throw a NullReferenceException, where ?.CategoryIsObsolete will return null. If you know for a fact that there will be a match then just use First, which cannot return null and then you don't need to use ?..
 
Solution
In that case, the code you claimed is the solution is actually not. As suggested, that will actually return an enumerable list, not a single value. FirstOrDefault is indeed the correct option, but you need to use it properly. If you want the CategoryIsObsolete value from the row with a specific CategoryId value then that's what you need to write code to get:
C#:
var name = categories.FirstOtDefault(i => i.CategoryId == 0)?.CategoryIsObsolete;
Note that I have used ?. rather than just . for the final property access. That's because the whole point of FirstOtDefault is that it might return null, so you have to allow for that. If it does then .CategoryIsObsolete would throw a NullReferenceException, where ?.CategoryIsObsolete will return null. If you know for a fact that there will be a match then just use First, which cannot return null and then you don't need to use ?..
Thank you very much. It works perfect :)
 
Back
Top Bottom