I have a list:
List<MyClass> lstClass1;
Where MyClass has 2 simple string property:
class MyClass
{
public string property1 { get; set; }
public string property2 { get; set; }
}
And I have a table on DB, MyTable, to query, where there are two string type columns:
MyTable
column MainKey
column AlternativeKey
For my script, i must select a join of DB and the list, with the following rule: if AlternativeKey exist, select the row if the first 4 characters in AlternativeKey equal to MyClass.property1 or MyClass.Property2, else select the row if the first 4 characters in MainKey equal to MyClass.property1 or MyClass.Property2. This is my implementation:
IQueryable<MyTable> source = getMyTable();
List<MyClass> lstClass1 = getListClass();
IQueryable<MyClass> qMyClassList = lstClass1.AsQueryable<MyClass>();
IQueryable<MyTable> selAlternative = from alt in source
join cl1 in qMyClassList on
alt.AlternativeKey.Substring(0, 4)
equals cl1.property1
join cl2 in qMyClassList on
alt.AlternativeKey.Substring(0, 4)
equals cl2.property2
where alt.AlternativeKey != null && alt.AlternativeKey.Length >= 4
select alt;
IQueryable<MyTable> selMain = from main in source
join cl1 in qMyClassList on
main.MainKey.Substring(0, 4)
equals cl1.property1
join cl2 in qMyClassList on
main.MainKey.Substring(0, 4)
equals cl2.property2
where main.AlternativeKey == null && main.MainKey.Length >= 4
select main;
source = alt.Union(main);
In execution, this query raise this exception when i loop on the result element:
Unable to create a constant value of type 'MyTable+MyClass'. Only primitive types or enumeration types are supported in this context.
What i'm doing wrong?