Alex Rivera | Logout

How to return anonymous type from c# method that uses LINQ to SQL

Asked 2009-07-01T18:03:52.577
44

Possible Duplicate:
LINQ to SQL: Return anonymous type?

I have a standard LINQ to SQL query, which returns the data as an anonymous type (containing about 6 columns of data of various datatypes).

I would like to make this returned object available to other parts of the program, either by returning it to the method-caller, or by assigning it to a property of the object containing the method.

How can I do this given that it is an anonymous type ("var")?

EDIT - Here is the code:

    using (ormDataContext context = new ormDataContext(connStr))
    {
        var electionInfo = from t1 in context.elections
                   join t2 in context.election_status
                   on t1.statusID equals t2.statusID
                   select new { t1, t2 };
    }
Edit
Report

1 Answer

5

Jon Skeet wrote a blog on how to do this, which is quite rightly titled Horribly Grotty Hack. Just as the title suggests, you really shouldn't be looking for ways to return an anonymous type. Instead, you should be creating a type that can be returned as this is the correct way to implement this feature.

Therefore, I recommend you create a concrete definition of the type to be returned and then populate that in your query for it to be returned.

answered 2009-07-01T18:10:21.210

Your Answer