1

我有这种方法,它工作了一段时间

public string getSlotText(int slotID)
{
    DataClasses1DataContext context = new DataClasses1DataContext();

    var slotString = context.spGetSlotTextBySlotID(slotID);

    return slotString.ElementAt(0).slotText;

}

但我现在真正想要的是

public var getSlotText(int slotID)
{
    DataClasses1DataContext context = new DataClasses1DataContext();

    var slotString = context.spGetSlotTextBySlotID(slotID);

    return slotString;
}

因为 slotString 中包含多个元素。我看到了一些其他示例,但没有一个使用 LINQ 调用存储过程的示例。

任何帮助都会很棒,我将不胜感激。

非常感谢

蒂姆

4

1 回答 1

0

var关键字不允许作为返回类型。它只能在初始化的方法和类成员中使用。

虽然有可能对返回类型进行类型推断,这会违反 C# 中的其他一些事情。例如匿名类型。由于方法不能在 C# 中返回匿名类型,因此您需要再次检查您不能返回匿名类型,而不仅仅是禁止将var关键字作为返回类型。

此外,allowvar作为返回类型会使方法签名在更改实现时不太稳定。

编辑:目前还不清楚您期望返回什么,但这里有一些标准解决方案:

如果slotText要从结果中返回所有内容:

return context.spGetSlotTextBySlotID(slotID).Select(s=> s.slotText).ToList());

(将返回一个List<string>

或者,如果您想返回所有SlotText(或从存储过程返回的任何类型)

return context.spGetSlotTextBySlotID(slotID).Select(s=> s.slotText).ToList());

(将返回一个List<SlotText>

于 2010-08-27T14:15:07.940 回答