使用 LINQ 调用存储过程,如何从方法传回 var?

发布于 2024-09-16 22:18:24 字数 619 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-09-23 22:18:24

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

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

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

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

如果您想从结果中返回所有slotText:(

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

将返回一个List

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

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

(将返回一个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:

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

(will return a List<string>)

Or if you want to return all SlotText (or whatever type that is return from the sproc)

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

(will return a List<SlotText>)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文