I have a method that takes an IQueryable. Is there a LINQ query that will give me back the size of each column in the IQuerable?
To be more clear: this is Linq-to-objects. I want to get the length of the ToString() of each "column".
I have a method that takes an IQueryable. Is there a LINQ query that will give me back the size of each column in the IQuerable?
To be more clear: this is Linq-to-objects. I want to get the length of the ToString() of each "column".
It depends. If you mean is there a completely generic way to make this determination the answer is no. All IQueryable will give access to is the Type of each expression. There is no way to arbitrarily map a Type to a column size.
If on the other hand you have the ability to map a Type to members and member type to a column size then yes there is a way to get the size.
public IEnumerable GetColumnSize(IQueryable source)
{
var types = MapTypeToMembers(source).Select(x => x.Type);
return types.Select(x => MapTypeToSize(x));
}
I guess you're talking about LINQ-to-SQL. It completely ignores column sizes. varchar(15), char(20) and nvarchar(max) are just strings for it. The overflow error will appear only on the SQL Server side.
You said for each "column", that would map to each property. In Linq to Objects, this should work, although too manual:
var lengths = from o in myObjectCollection
select new
{
PropertyLength1 = o.Property1.ToString().Length,
PropertyLength2 = o.Property2.ToString().Length,
...
}
If you meant the ToString() length of each item ("row") in the collection, then:
var lengths = from o in myObjectCollection
select o.ToString().Length;