Using LINQ calling a sproc, how can I pass a var back from a method?
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)
{开发者_Python百科
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
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>
)
精彩评论