Solo
Solo

Reputation: 579

LINQ to SQL C# function with int and String

I have a LINQ to SQL query function combinding to tables, but the function gives an error on "return res.ToList();".

My function:

List<T> GetAllInitialize()
{
    PositionDataClassesDataContext context = new PositionDataClassesDataContext();

    var res = from positions in context.it_positions
              join vessels in context.it_vessels on positions.imo_no equals vessels.imo_no
              select new { positions.imo_no, positions.position_cordinates, vessels.vessel_id, vessels.equipment };

    return res.ToList();
}

enter image description here

Upvotes: 2

Views: 313

Answers (3)

Brandon Moretz
Brandon Moretz

Reputation: 7631

You can't return an anonymous type in a method signature with a generic type argument.

You can:

  1. Have your return signature List<object>, in which case you can't access any of the fields, which you would therefor need reflection to access your field values.
  2. Return List<dynamic> and lose compile type type checking on field references, however they would be "accessible".
  3. Declare an intermediate concrete type that's populated by your query and specified as the return type in your method signature.

The third option is really the only viable choice here in my opinion.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

Although you can use anonymous types or collections of objects of anonymous type locally with static typing, there is no way to return them from a function without losing the information about their static type.

If you cannot or for some reason prefer not to create a named class to hold the return data, you need to either return List<dynamic>, or List<object>. Both these approaches have their drawbacks: dynamic will be slower than a comparable statically-typed object, while System.Object would not let you do much with the data that you get back.

The best solution would be creating a named return type:

public class PositionData {
     public int ImoNo {get;set;}
     public string PositionCordinates {get;set;}
     public int VesselId {get;set;}
     public string Equipment  {get;set;}
}

Now your query will look like this:

select new PositionData {
    ImoNo = positions.imo_no
,   PositionCordinates = positions.position_cordinates
,   VesselId = vessels.vessel_id
,   Equipment = vessels.equipment
};

The return type of your method would change to List<PositionData>.

Upvotes: 1

Servy
Servy

Reputation: 203827

  1. Create a new class that contains four properties that represent those fields that your anonymous object has.

  2. Alter the select method to create instances of this new custom type rather than using an anonymous type.

  3. Alter the return type of the method from returning List<T> to returning a list of that custom type.

Anonymous objects are not designed to be used outside of the scope in which they were created. Because you intend to use this type from multiple scopes you should create a new named type to represent this data.

Upvotes: 0

Related Questions