Linq to SQL with Sproc and Variable Columns?
I have a sproc that returns at least 4 columns and then n columns after that (it's the result of a Pivot).
I can't create a quick Type to declare the result set shape to use with Linqtosql because I don't 开发者_Python百科actually know what the shape will be.
Since Linqtosql is my DAL, I want to stick with it if possible - I don't want to back to untyped DataSets, do I?
How can I get LinqToSql to return a sproc result with a variable number of properties/columns?
Thanks.
LINQ-to-SQL needs to map your database objects to concrete types. So, in short, you can't do this.
There are workarounds. I expect that the best outcome would be to map your n columns to a collection inside each data object.
If you can change your database schema to instead use a parent-child View pattern, rather than a single dynamic sproc, the LINQ-to-SQL will be able to map this. For example, you'd have your first 4 columns in a 'parent' view, and each subsequent column in your existing proc would become a row in a 'child' view which needs to reference the parent view.
Another alternative - simpler but dirtier - would be to return a single column containing your current n columns in CSV format (or other delimiter). You then parse this single value into a collection. Ugly but simple.
精彩评论