使用 LINQ 调用存储过程,如何从方法传回 var?
我有这种方法,它工作了一段时间
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 调用存储过程。
任何帮助都会很棒,我将非常感激。
非常感谢
蒂姆
I have this method which worked for a while
public string getSlotText(int slotID)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var slotString = context.spGetSlotTextBySlotID(slotID);
return slotString.ElementAt(0).slotText;
}
But what i really want now is something like
public var getSlotText(int slotID)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var slotString = context.spGetSlotTextBySlotID(slotID);
return slotString;
}
as slotString has more than one element within it. I saw some other examples but none with LINQ calling a sproc.
Any help would be amazing, id be very grateful.
Many Thanks
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不允许使用
var
关键字作为返回类型。它只能在带有初始化的方法和类成员内部使用。虽然可以对返回类型进行类型推断,但这会违反 C# 中的其他一些规则。例如匿名类型。由于方法不能返回 C# 中的匿名类型,因此您需要再次检查是否不能返回匿名类型,而不仅仅是禁止
var
关键字作为返回类型。此外,允许 var 作为返回类型会使方法签名在更改实现时不太稳定。
编辑:有点不清楚您期望返回什么,但这里有一些标准解决方案:
如果您想从结果中返回所有
slotText
:(将返回一个
List
)或者如果您想返回所有
SlotText
(或从存储过程返回的任何类型)(将返回一个
List
>)The
var
keyword is not allowed as a return type. It may only be used inside methods and class members with initialization.While it could be possible to do type inference for the return type that would violate some other things in C#. For example anonymous types. Since methods can not return anonymous types in C# you would need another check that you can not return anonymous types than just forbidding the
var
keyword as a return type.Besides, allowing
var
as a return type would make the method signature less stable when changing the implementation.Edit: It is somewhat unclear what you expect to return, but here are some standard solutions:
If you want to return all
slotText
from the result:(will return a
List<string>
)Or if you want to return all
SlotText
(or whatever type that is return from the sproc)(will return a
List<SlotText>
)