mardi 16 février 2016

Linq and join to different properites

I have a few enitities which I would like to join by linq. The problem is that property on which I would like to join is dynamic. For example:

public class AClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class BClass
{
    public int Id { get; set; }
    public int AClassId{ get; set; }
    public virtual AClass A { get; set; }

    public string String_Value_1 { get; set; }
    public string String_Value_2 { get; set; }
    public string String_Value_3 { get; set; }
}

public class Mapping
{
    public int Id { get; set; }
    public int AClassId{ get; set; }
    public virtual AClass A { get; set; }

    public string ColumnDesc{ get; set; }
    public string ColumnMappingName { get; set; }
}

and for example in my table of AClass will be stored "n" - elemnts, each element can haved ifferent mapping of source where string data is stored.

product_id = 1 will have mapping {ColumnDesc = "x" and ColumnMappingName = "String_Value_1"}

// this means that value of property "x" is stored for this product in column String_Value_1

product_id = 2 will have mapping {ColumnDesc = "x" and ColumnMappingName = "String_Value_2"}

// this means that value of property "x" is stored for this product in column String_Value_2

product_id = 3 will have mapping {ColumnDesc = "x" and ColumnMappingName = "String_Value_3"}

// this means that value of property "x" is stored for this product in column String_Value_3

product_id = 4 will have mapping {ColumnDesc = "x" and ColumnMappingName = "String_Value_1"}

// this means that value of property "x" is stored for this product in column String_Value_1

What I would like to do, is to get all values (from BClass) for AClass objects which have defined CoulmnDesc. I do not want to do it by 3 queries (string columns can be 10 or even more) but rather to do it dinamicly by joining by property name or reflection with using lambda expresions





Aucun commentaire:

Enregistrer un commentaire