Please add a method With to be able to change values of a few object properties only. Imagine you have a table with 30 columns C1, C2, … C30.
var q = from t in db.Tbl
select t.With(new Tbl { C13 = t.C7});
This should work the same as
var q = from t in db.Tbl
select new Tbl
{
C1 = t.C1,
C2 = t.C2,
...
C12 = t.C12,
C13 = t.C7, // <-- !
C14 = t.C14,
...
C30 = t.C30
};
So you don’t need to copy the long list of properties just to change a single value.
With expressions introduced in C# 9 seems aren’t allowed in expression trees so we need an extension method With.
============================
[Extension("", ServerSideOnly = true, BuilderType = typeof(WithBuilder))]
public static T With<T>(this T @this, T newInit) =>
throw new LinqException($"'{nameof(With)}' is only server-side method.");
class WithBuilder : IExtensionCallBuilder
{
public void Build(ISqExtensionBuilder builder)
{
Expression? @this = builder.Arguments[0];
MemberInitExpression? newInit = builder.Arguments[1] as MemberInitExpression ?? throw new ArgumentException();
// ...
builder.ResultExpression = ???;
}
}
The type of builder.ResultExpression is ISqlExpression. I can create another MemberInitExpression that will copy all properties from @this except the ones used in newInit. But I couldn’t create a correct value of builder.ResultExpression.
It would be nice if Linq2Db provided a possibility to replace expression tree(s) with another expression tree (some IExtensionCallBuilder2 which ResultExpression type is also a System.Linq.Expressions.Expression) so you wouldn’t need to manually create the lower-level ISqlExpressions. And that should work on some earlier stage of the query processing.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
You can Implement
IExpressionPreprocessor
in your DataConnection class.The following (incomplete) solution works:
Code
Usage
The
Process
method replaces all occurrences of the methods with RawExpressionMethod attribute with the results of the Impl methods. It should be called before the engine does some caching etc. I think it would be nice if Linq2Db provided something like RawExpressionMethodAttribute and executed the replacing Visitor by itself.