-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Support Cast/As for non enumerable properties #566
Comments
Hello @dsidedp , did you also investigate if the See below links on how to use it: |
Maybe I miss something, but I was not able to cast property of |
@dsidedp |
This is not the case. In example you referred to, cast is applied to If BTW, here is the similar issue #519 Here is the simple test for this case public void Test1()
{
// Assign
var qry = new[]
{
new { Employee = (BaseEmployee)new Worker { Name = "1", Other = "x" }},
new { Employee = (BaseEmployee)new Boss { Name = "2", Function = "y" }}
}.AsQueryable();
// Act
var cast = qry.Count(x=>(x.Employee as Worker).Other == "x");
var castDynamic = ????
// Assert
Check.That(cast).Equals(castDynamic);
} Or similar to one you referred to but for case when property is an object. public void Test2()
{
// Assign
var qry = new[]
{
new { Employee = (BaseEmployee)new Worker { Name = "1", Other = "x" }},
new { Employee = (BaseEmployee)new Boss { Name = "2", Function = "y" }}
}.AsQueryable();
// Act
var cast = qry.Select(x=>(x.Employee is Worker) ? (x.Employee as Worker).Other : "-").ToArray();
var castDynamic = ????
// Assert
Check.That(cast.Length).Equals(castDynamic.Length);
} |
@dsidedp This is not yet supported, but this can be added. Would this syntax be ok? [Fact]
public void IsAndCastToType_Dynamic_ActingOnProperty_And_GetProperty()
{
// Assign
var qry = new []
{
new EmployeeWrapper { Employee = new Worker { Name = "1", Other = "x" } },
new EmployeeWrapper { Employee = new Boss { Name = "2", Function = "y" } }
}.AsQueryable();
// Act
var cast = qry.Select(c => c.Employee is Worker ? ((Worker) c.Employee).Other : "-").ToArray();
var castDynamic = qry.Select("iif(Is(Employee, \"System.Linq.Dynamic.Core.Tests.Entities.Worker\"), Cast(Employee, \"System.Linq.Dynamic.Core.Tests.Entities.Worker\").Other, \"-\")").ToDynamicArray();
// Assert
Check.That(cast.Length).Equals(castDynamic.Length);
} |
Sure. IMHO, that's the simplest and most obvious syntax. (assuming simple type resolution will be supported here) |
simple type resolution will be supported with: var config = new ParsingConfig
{
ResolveTypesBySimpleName = true
}; |
A new version will be released soon. |
Thank you for the fast update. |
Consider the following
As far as I understand, currently, it is not possible to filter
List<SomeClass>
based onSomeClass.Item
derived class property value since there is no way to cast non-enumerable propertySomeClass.Item
toDerivedClass1
orDerivedClass2
:Please consider supporting Cast/As/Is for non-enumerable properties.
result1
and correspondinglinq1
cases are very useful when parsed expression is passed to EF or other ORMs with inheritance support.Thanks.
The text was updated successfully, but these errors were encountered: