Further Linq enhancements
(Created page with "Up until now there has been a problem to filter on type in Linq The problem can be shown with this model: o867546789076543mnvjhgcfdt If you want to find SomeSubClass that h...") |
(Automatically adding template at the end of the page.) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
Until now, it has been problematic to filter on type in Linq. | |||
This model shows the problem: | |||
[[File:Linq.png|none|thumb|366x366px]] | |||
If you want to find SomeSubClass that has a Class2 with a specific name:<html> | |||
<pre class="code"><span style="background: white; color: blue;">var </span><span style="background: white; color: black;">foo2 = (</span><span style="background: white; color: blue;">from </span><span style="background: white; color: black;">v </span><span style="background: white; color: blue;">in </span><span style="background: white; color: #2b91af;">EcoLinqExtender</span><span style="background: white; color: black;">.PSQuery<</span><span style="background: white; color: #2b91af;">Class2</span><span style="background: white; color: black;">>(EcoSpace) | |||
</span><span style="background: white; color: blue;">where </span><span style="background: white; color: black;">(v.Class1.Attribute1 == </span><span style="background: white; color: #a31515;">"5" </span><span style="background: white; color: black;">&& v.Name == </span><span style="background: white; color: #a31515;">"5A"</span><span style="background: white; color: black;">) | |||
</span><span style="background: white; color: blue;">select </span><span style="background: white; color: black;">v.Class1)</span></pre> | |||
</html> | |||
But now, you also get Class1s that are not of SomeSubClass.That is a problem in certain situations. | |||
In Ocl2Ps, we would have done: | |||
''Class2.allInstances->select(a|((a.Class1.Attribute1 = '5') and (a.Name = '5A'))).Class1->FilterOnType(SomeSubClass)'' | |||
Until now, MDriven Linq2Sql logic did not understand the Linq counterpart of FilterOnType – which is OfType<class>(). | |||
But now it does: | |||
<html> | |||
<pre class="code"><span style="background: white; color: blue;">var </span><span style="background: white; color: black;">foo2 = (</span><span style="background: white; color: blue;">from </span><span style="background: white; color: black;">v </span><span style="background: white; color: blue;">in </span><span style="background: white; color: #2b91af;">EcoLinqExtender</span><span style="background: white; color: black;">.PSQuery<</span><span style="background: white; color: #2b91af;">Class2</span><span style="background: white; color: black;">>(EcoSpace) | |||
</span><span style="background: white; color: blue;">where </span><span style="background: white; color: black;">(v.Class1.Attribute1 == </span><span style="background: white; color: #a31515;">"5" </span><span style="background: white; color: black;">&& v.Name == </span><span style="background: white; color: #a31515;">"5A"</span><span style="background: white; color: black;">) | |||
</span><span style="background: white; color: blue;"> select </span><span style="background: white; color: black;">v.Class1).</span><span style="background: white; color: black;"><span style="background-color: #ffff00;">OfType<</span></span><span style="background-color: #ffff00;"><span style="background: white; color: #2b91af;">SomeSubClass</span><span style="background: white; color: black;">>()</span></span></pre> | |||
</html> | |||
With this | With this in place, the database takes care of all the filtering for us and returns the SomeSubClasses that have any Class2 with the correct criteria. | ||
[[Category:Linq]] | |||
{{Edited|July|12|2024}} |
Latest revision as of 15:34, 10 February 2024
Until now, it has been problematic to filter on type in Linq.
This model shows the problem:
If you want to find SomeSubClass that has a Class2 with a specific name:
var foo2 = (from v in EcoLinqExtender.PSQuery<Class2>(EcoSpace) where (v.Class1.Attribute1 == "5" && v.Name == "5A") select v.Class1)
But now, you also get Class1s that are not of SomeSubClass.That is a problem in certain situations.
In Ocl2Ps, we would have done:
Class2.allInstances->select(a|((a.Class1.Attribute1 = '5') and (a.Name = '5A'))).Class1->FilterOnType(SomeSubClass)
Until now, MDriven Linq2Sql logic did not understand the Linq counterpart of FilterOnType – which is OfType<class>().
But now it does:
var foo2 = (from v in EcoLinqExtender.PSQuery<Class2>(EcoSpace) where (v.Class1.Attribute1 == "5" && v.Name == "5A") select v.Class1).OfType<SomeSubClass>()
With this in place, the database takes care of all the filtering for us and returns the SomeSubClasses that have any Class2 with the correct criteria.
This page was edited more than 11 months ago on 02/10/2024. What links here