从foreach循环返回多个项目

时间:2022-01-06 19:06:18

I would like to ask how I can return just the for each loop

我想问一下如何才能为每个循环返回

current im getting a not all code paths return a value .

当前即时通讯获取并非所有代码路径都返回一个值。

public Config getConfigSingle(string CID, string name)
{
    var raw = db.ap_GetInfo(CID, name);

    foreach (var item in raw.ToList())
    {
        return new Config
        {
            Name = item.Name.ToString(),
            Value = item.Value.ToString(),
        };
    }       
}

public partial class ClubConfig
{
    public string Name { get; set; }
    public string Value { get; set; }
}

Thanks M

3 个解决方案

#1


You can use yield:

你可以使用yield:

public IEnumerable<Config> getConfigSingle(string CID, string name)
{
    var raw = db.ap_GetInfo(CID, name);

    foreach (var item in raw.ToList())
    {
        yield return new Config
        {
            Name = item.Name.ToString(),
            Value = item.Value.ToString(),
        };
    }       
}

#2


you can use LINQ to build your object to return:

您可以使用LINQ构建您的对象以返回:

public List<Config> getConfigSingle(string CID, string name)
{
var raw = db.ap_GetInfo(CID, name);

return raw.Select(r => new Config 
                        { 
                           Name = r.Name.ToString(), 
                           Value = r.Value.ToString()
                        }).ToList();

}

#3


You must return a value for each code path. If you collectio in the foreach is empty, the method would return nothing. Add a return null at the end.

您必须为每个代码路径返回一个值。如果foreach中的collectio为空,则该方法不会返回任何内容。最后添加一个返回null。

public Config getConfigSingle(string CID, string name)
{
    var raw = db.ap_GetInfo(CID, name);

    foreach (var item in raw.ToList())
    {
        return new Config
        {
            Name = item.Name.ToString(),
            Value = item.Value.ToString(),
        };
    }  
    return null;
}

#1


You can use yield:

你可以使用yield:

public IEnumerable<Config> getConfigSingle(string CID, string name)
{
    var raw = db.ap_GetInfo(CID, name);

    foreach (var item in raw.ToList())
    {
        yield return new Config
        {
            Name = item.Name.ToString(),
            Value = item.Value.ToString(),
        };
    }       
}

#2


you can use LINQ to build your object to return:

您可以使用LINQ构建您的对象以返回:

public List<Config> getConfigSingle(string CID, string name)
{
var raw = db.ap_GetInfo(CID, name);

return raw.Select(r => new Config 
                        { 
                           Name = r.Name.ToString(), 
                           Value = r.Value.ToString()
                        }).ToList();

}

#3


You must return a value for each code path. If you collectio in the foreach is empty, the method would return nothing. Add a return null at the end.

您必须为每个代码路径返回一个值。如果foreach中的collectio为空,则该方法不会返回任何内容。最后添加一个返回null。

public Config getConfigSingle(string CID, string name)
{
    var raw = db.ap_GetInfo(CID, name);

    foreach (var item in raw.ToList())
    {
        return new Config
        {
            Name = item.Name.ToString(),
            Value = item.Value.ToString(),
        };
    }  
    return null;
}