查询从单行找到福彩12选5走势图对象

Simsen.vejle.

活跃的成员
加入
2月14日,2021年
消息
38
编程经验
Beginner
你好,
我有问题要找到正确的语法,以获得单行的单个结果。

我收到行(类别),我可以在此行中看到有福彩12选5走势图类别代码字段(它是我想要的价值)

但是在制作AF Firstordefault查询类别结果时 - 而且我不知道为什么。



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);
                    }
                }

也许我正在做太多 - 但我有先得得出行 - 然后我想要这个行中的条目的值。

此致
Simsen. :)
 
Solution
是的,我知道,只有福彩12选5走势图类别 - 它是类别的主要键 :)
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...

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,969
地点
切萨皮克,va.
编程经验
10+
但是,您正在进行的内容在您请求时没有返回单行的单个对象。您可以获得具有类别的所有类别的IEnumerabled == 0.您是否只是在假设CategoryID是福彩12选5走势图唯一的价值,并且只有一行将满足该状况?
 

Simsen.vejle.

活跃的成员
加入
2月14日,2021年
消息
38
编程经验
Beginner
但是,您正在进行的内容在您请求时没有返回单行的单个对象。您可以获得具有类别的所有类别的IEnumerabled == 0.您是否只是在假设CategoryID是福彩12选5走势图唯一的价值,并且只有一行将满足该状况?
是的,我知道,只有福彩12选5走势图类别 - 它是类别的主要键 :)
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,969
地点
切萨皮克,va.
编程经验
10+
DAL(数据访问层)应该保存到持久性,并从持久性加载。您的域对象不应该具有ID。

 

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,751
地点
悉尼,澳大利亚
编程经验
10+
是的,我知道,只有福彩12选5走势图类别 - 它是类别的主要键 :)
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 ?..
 
解决方案

Simsen.vejle.

活跃的成员
加入
2月14日,2021年
消息
38
编程经验
Beginner
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 ?..
非常感谢你。它很完美 :)
 
最佳 底部