That moment when a lot of difficult work becomes a five minute job...
-
Looks like stuff for an upcoming article :thumbsup:
A tip at best... Between work, my own business, study for Azure exams, and something that more or less resembles a social life I have zero time to write at the moment :sigh:
Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
A tip at best... Between work, my own business, study for Azure exams, and something that more or less resembles a social life I have zero time to write at the moment :sigh:
Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
Looks like we are in the same boat again (don't know if that's good English), time keeps on slippin, slippin, into the future ... :-\
-
Is the additional info coming from the same entity. If so, why not just add it to TModel and get it with the first select?
"Time flies like an arrow. Fruit flies like a banana."
It's actually more like
context.Set().Select(e => new TModel { ... }).ToList();
And now I want to "open up" the selector, not completely replace it (because that will result in quite some duplicated code), but add to it. I have xTEntity
classes that share stuff likeId
,Name
,Active
and even a details table reference, that go nicely into the common selector. But some also have one or two properties that aren't shared by the others. Other than those few properties all logic is exactly the same. I currently have five web pages that use the exact same code in the back-end (save for those few properties) :D Took me about twenty to thirty hours to write the first page as abstract as it is and then it took me an hour to add three more and I just spend four hours for the fifth, for which I had to add this :laugh:Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
It's actually more like
context.Set().Select(e => new TModel { ... }).ToList();
And now I want to "open up" the selector, not completely replace it (because that will result in quite some duplicated code), but add to it. I have xTEntity
classes that share stuff likeId
,Name
,Active
and even a details table reference, that go nicely into the common selector. But some also have one or two properties that aren't shared by the others. Other than those few properties all logic is exactly the same. I currently have five web pages that use the exact same code in the back-end (save for those few properties) :D Took me about twenty to thirty hours to write the first page as abstract as it is and then it took me an hour to add three more and I just spend four hours for the fifth, for which I had to add this :laugh:Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
You could do something with the Join method but I would derive an new model from TModel. There are a number of ways to do what you want, but without seeing your existing code, I can't suggest the best way. You really want this to only make one call to the database, so getting the right projection in the first place is important.
-
So I wanted to combine two select expressions in EF Core 2.0. Something like
context.MyTable.Select(e => new TModel { ... }).Select(additionalSelector).ToList();
Now that's just not possible, because the firstSelect
converts the query toIQueryable
and the secondSelect
has no reference to the entity (unless I put it in the model, not sure if that works, but I don't want to anyway). So I found myself "merging" two select expressions which went awry because the "e" inadditionalSelector
isn't the same "e" as in the original selector [read: reference toParameterExpression
]. AndExpressions
are immutable... :sigh: So I wanted to start by converting the entire tree manually (and probably drop the idea because that isn't worth it) when I found the ExpressionVisitor[^]. All that manual work pretty much became:public class ExpressionParameterSwitcher : ExpressionVisitor { private readonly ParameterExpression parameter; public ExpressionParameterSwitcher(ParameterExpression parameter) { this.parameter = parameter; } protected override Expression VisitParameter(ParameterExpression node) { return parameter; } }
Good times :D Thought I'd post it here in case anyone ever needs it and, like me, didn't know about the
ExpressionVisitor
.Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
Bookmarked. This might come in handy if i ever rebuild my caching class. I just wish you could've given the thread a more descriptive name, next time I see it among the bookmarks I won't have a clue what it is about. :)
Wrong is evil and must be defeated. - Jeff Ello
-
A tip at best... Between work, my own business, study for Azure exams, and something that more or less resembles a social life I have zero time to write at the moment :sigh:
Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
That's what I was gonna suggest - make it a tip/trick.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
So I wanted to combine two select expressions in EF Core 2.0. Something like
context.MyTable.Select(e => new TModel { ... }).Select(additionalSelector).ToList();
Now that's just not possible, because the firstSelect
converts the query toIQueryable
and the secondSelect
has no reference to the entity (unless I put it in the model, not sure if that works, but I don't want to anyway). So I found myself "merging" two select expressions which went awry because the "e" inadditionalSelector
isn't the same "e" as in the original selector [read: reference toParameterExpression
]. AndExpressions
are immutable... :sigh: So I wanted to start by converting the entire tree manually (and probably drop the idea because that isn't worth it) when I found the ExpressionVisitor[^]. All that manual work pretty much became:public class ExpressionParameterSwitcher : ExpressionVisitor { private readonly ParameterExpression parameter; public ExpressionParameterSwitcher(ParameterExpression parameter) { this.parameter = parameter; } protected override Expression VisitParameter(ParameterExpression node) { return parameter; } }
Good times :D Thought I'd post it here in case anyone ever needs it and, like me, didn't know about the
ExpressionVisitor
.Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
I have experienced the opposite quite often. You know, "that moment you realize that what you thought would be a five minute task, is turning into a lot of work". :sigh: :confused::mad:
"Newer" is NOT automatically better, only Different. (And more complex and bug ridden when it comes to all of the "boutique" languages / frameworks out there)
-
I have experienced the opposite quite often. You know, "that moment you realize that what you thought would be a five minute task, is turning into a lot of work". :sigh: :confused::mad:
"Newer" is NOT automatically better, only Different. (And more complex and bug ridden when it comes to all of the "boutique" languages / frameworks out there)
That's also very familiar :laugh:
Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
You could do something with the Join method but I would derive an new model from TModel. There are a number of ways to do what you want, but without seeing your existing code, I can't suggest the best way. You really want this to only make one call to the database, so getting the right projection in the first place is important.
Matthew@Home wrote:
but without seeing your existing code, I can't suggest the best way
Well, I wasn't really asking for suggestions, but I appreciate the help :) Here's the situation: I've got a couple of entities.
public class EntityA : IMyInterface
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public List Details { get; set; }
}public class EntityB : IMyInterface
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public List Details { get; set; }
}public class EntityC : IMyInterface
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public string SomeOtherProperty { get; set; }
public string AnotherProperty { get; set; }
public List Details { get; set; }
}// Details all pretty much look the same as well, I've got more entities...
Then I've got this model:
public class SharedModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public List Details { get; set; }
}And a query for all entities:
public TModel GetModels()
where TModel : SharedModel, new()
where TEntity : class, IMyInterface, new()
{
context.Set()
.Where(...)
.Select(e => new TModel
{
Id = e.Id,
Name = e.Name,
Active = e.Active,
Details = e.Details.Select(...)
}).ToList();
}So this works for everything except
SomeOtherProperty
andAnotherProperty
forEntityC
. So I somehow want to inject them into the selector.Expression> baseSelect = e => new TModel
{
Id = e.Id,
Name = e.Name,
Active = e.Active,
Details = e.Details.Select(...)
};// And in inherited class, call GetModels().
// Use this as parameter.
Expression> additionalSelect = e => new MyCustomModel
{
SomeOtherProperty = e.SomeOtherProperty,
AnotherProperty = e.AnotherProperty
};And then merge the two trees...
MemberInitExpression baseSelectBody = (MemberInitExpression)baseSelect.Body;
MemberInitExpression additionalSelectBody -
That's also very familiar :laugh:
Best, Sander Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
Yip.. something that should be fast takes forever. Uhg And then having to explain to management :doh: why...