gpt4 book ai didi

c# - 返回具有空值的类

转载 作者:行者123 更新时间:2023-12-02 21:37:18 24 4
gpt4 key购买 nike

我有以下代码。
如果没有 WType 为空,我喜欢返回一个空类,其中 Key 和 Value 为空字符串。我收到一个错误,其中包含“;”是期待。

返回类型必须是:

IEnumerable<WportType>

这是代码:

if (wType == "Riconda")
{
return dbContext.data_LookupValues
.Where(w => w.Category == "WLoc")
.OrderBy(w => w.SortId)
.Select(a => new WportType
{
Key = a.Key,
Value = a.Value
});

}
else
{
return WportType
{
Key = "",
Value = ""
};
}

最佳答案

看来您的方法返回 IEnumerable<WportType> 。您正在尝试退回单个商品。您需要将其包装在一个集合中:

如下:

    if (wType == "Riconda")
{
return dbContext.data_LookupValues
.Where(w => w.Category == "WLoc")
.OrderBy(w => w.SortId)
.Select(a => new WportType
{
Key = a.Key,
Value = a.Value
});

}
else
{
return new[] { new WportType { Key = "", Value = "" } };
}

关于c# - 返回具有空值的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21099203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com