views:

26

answers:

1

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

A: 

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>)

Albin Sunnanbo
Hi Albin thanks for your reply.I basically want to do something like this I basically want to create a method in a class that calls a sproc via link, then in a seperate page call that method so it returns an object that i can then take the values out and display them on my page.someType myVar WebApplication1.getSlotText(1)textbox1.text = myVar.slotTitle.toString();textbox2.Text = myVar.slotText.toString();textbox3.Text = myVar.slotUrl.toString();I will look through your answer but thought i'd post that up in the meantime.thanks again
Tim Butler
@Tim: Can you edit your question with this information? You should also clarify if you want one object with different properties or many objects with a single property or many objects with many properties. You may post an example of the expected content of the return value.
Albin Sunnanbo