<p>I've been using Linq to SQL for some time now and I find it to be really helpful and easy to use. With other ORM tools I've used in the past, the entity object filled from the database normally has a property indicating the length of the underlying data column in the database. This is helpful in databinding situations where you can set the MaxLength property on a textbox, for example, to limit the length of input entered by the user.</p>

<p>I cannot find a way using Linq to SQL to obtain the length of an underlying data column. Does anyone know of a way to do this? Help please.</p>

[{'answer_id': 20694, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 4, 'selected': True, 'text': '<p>Using the LINQ ColumnAttribute to Get Field Lengths from your Database :</p>\n\n<p><a href="http://www.codeproject.com/KB/cs/LinqColumnAttributeTricks.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/cs/LinqColumnAttributeTricks.aspx</a></p>\n'}, {'answer_id': 21212, 'author': 'jwalkerjr', 'author_id': 689, 'author_profile': 'https://Stackoverflow.com/users/689', 'pm_score': 2, 'selected': False, 'text': "<p>Thanks. Actually both of these answers seem to work. Unfortunately, they seem to look at the Linq attributes generated when the code-generation was done. Although that would seem to be the right thing to do, in my situation we sell software products and occasionally the customer will expand some columns lengths to accommodate their data. Thus, the length of the field as reported using this technique may not always reflect the true length of the underlying data column. Ah well, not Linq to SQL's fault, is it? :)</p>\n\n<p>Thanks for the quick answers!</p>\n"}, {'answer_id': 1397564, 'author': 'Paul Alexander', 'author_id': 76456, 'author_profile': 'https://Stackoverflow.com/users/76456', 'pm_score': 1, 'selected': False, 'text': '<p>If you need to know the exact column length you can resort to the System.Data classes themselves. Something a bit like this:</p>\n\n<pre><code>var context = new DataContextFromSomewhere();\nvar connection = context.Connection;\nvar command = connection.CreateCommand( "SELECT TOP 1 * FROM TableImInterestedIn" );\nvar reader = command.ExecuteReader();\nvar table = reader.GetSchemaTable();\n\nforeach( var column in table.Columns )\n{\n    Console.WriteLine( "Length: {0}", column.MaxLength );\n\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/20684', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/689/']