Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dissolve schema query into distinct ToDataTable paths #1007

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions MoreLinq/ToDataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,6 @@ static MemberInfo[] BuildOrBindSchema(DataTable table, MemberInfo[] members)

var columns = table.Columns;

var schemas = from m in members
select new
{
Member = m,
Type = (m.MemberType == MemberTypes.Property
? ((PropertyInfo)m).PropertyType
: ((FieldInfo)m).FieldType) switch
{
var type when Nullable.GetUnderlyingType(type) is { } t => t,
var type => type,
},
Column = columns[m.Name],
};

//
// If the table has no columns then build the schema.
// If it has columns then validate members against the columns
Expand All @@ -211,25 +197,34 @@ var type when Nullable.GetUnderlyingType(type) is { } t => t,

if (columns.Count == 0)
{
columns.AddRange(schemas.Select(m => new DataColumn(m.Member.Name, m.Type)).ToArray());
foreach (var member in members)
_ = columns.Add(member.Name, GetElementaryTypeOfPropertyOrField(member));

return members;
}
else
{
members = new MemberInfo[columns.Count];

foreach (var info in schemas)
{
var member = info.Member;
var column = info.Column ?? throw new ArgumentException($"Column named '{member.Name}' is missing.", nameof(table));
var columnMembers = new MemberInfo[columns.Count];

if (info.Type != column.DataType)
throw new ArgumentException($"Column named '{member.Name}' has wrong data type. It should be {info.Type} when it is {column.DataType}.", nameof(table));
foreach (var member in members)
{
var column = columns[member.Name] ?? throw new ArgumentException($"Column named '{member.Name}' is missing.", nameof(table));

members[column.Ordinal] = member;
}
if (GetElementaryTypeOfPropertyOrField(member) is var type && type != column.DataType)
throw new ArgumentException($"Column named '{member.Name}' has wrong data type. It should be {type} when it is {column.DataType}.", nameof(table));

columnMembers[column.Ordinal] = member;
}

return members;
return columnMembers;

static Type GetElementaryTypeOfPropertyOrField(MemberInfo member) =>
(member.MemberType == MemberTypes.Property ? ((PropertyInfo)member).PropertyType
: ((FieldInfo)member).FieldType)
switch
{
var type when Nullable.GetUnderlyingType(type) is { } ut => ut,
var type => type,
};
}

static Func<T, object[]> CreateShredder<T>(IEnumerable<MemberInfo> members)
Expand Down