Returning from a field collection with Dynamic library RRS feed

  • Question

  • Hi! we are developing a framework that through the one url, generate a query based on the mapped entities by entity framework.
    We are using the Dynamic Library ( http://lcs3.syr.edu/faculty/fawcett/handouts/CoreTechnologies/CSharp/samples/CSharpSamples/LinqSamples/DynamicQuery/Dynamic%20Expressions.html) and we are struggling to return to the fields of a relationship 1..N.

    Example :

    Based on this relationship, I need to accomplish the same idea of ​​following linq :

    var sql = from p in context.SomeTable
    select new { NAME = p.NAME, 
                 PHONES = p.TB_PHONE.Select(ph => ph.PHONE )

    Since I'm not working with typing , we chose to use dynamic library because apparently allowed us the flexibility to manipulate strings to return.
    Then following the same idea , we set up the following line :

    var sql = context.SomeTable.Select("new(TB_PEOPLE.TB_PHONE.PHONE)");

    In this case , the returns an error stating that the attribute " PHONE" does not exist " TB_PEOPLE " of course ! So much so that we try to say that this attribute belongs to the table " TB_PHONE " but he does not understand .

    So I ask you, how do I return to only certain fields of an entity where the relationship can be N ?
    also tried to call the method "Select " there but I am informed that this method can not be used .

    I do not know what else to do , any help will be appreciated !

    Thank you.

    Monday, November 11, 2013 7:22 PM


  • Hello,

    >>So I ask you, how do I return to only certain fields of an entity where the relationship can be N ?

    For this, as far as I know, we cannot achieve this us the dynamic library. Because the library is actually mapped to the entity class. And in the TB_PEOPLE entity there is no property named ‘PHONE’.

    We can just get the whole Type like below:

    var sql = context.SomeTable.Select("new(TB_PHONE)");

    Or Standard Query Operators as said in the link:

    seq . Where ( predicate )                            seq . Any ( )

    seq . Any ( predicate )                                 seq . All ( predicate )

    seq . Count ( )                                                seq . Count ( predicate )

    seq . Min ( selector )                                    seq . Max ( selector )

    seq . Sum ( selector )                                    seq . Average ( selector )

    So for achieving what you want, I think we need to use the general linq just what you have mentioned:

    var sql = from p in context.SomeTable
    select new { NAME = p.NAME, 
                 PHONES = p.TB_PHONE.Select(ph => ph.PHONE )

    And if you want to get the properties, I recommend to create a POCO class like:


    public class View
            public View ()
                TB_PHONE = new List<TB_PHONE>();
            public string NAME { get; set; }
            public ICollection<TB_PHONE> TB_PHONE { get; set; }

    And changing query to be

    var sql = from p in context.SomeTable
    select new View{ NAME = p.NAME, 
                 PHONES = p.TB_PHONE.Select(ph => ph.PHONE )

    Then we can get the query type which is View because it will return a dynamic type before and we cannot get the record row by row.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Leo (Apple) Yang Wednesday, November 20, 2013 7:47 AM
    • Marked as answer by Fred Bao Wednesday, November 20, 2013 8:05 AM
    Tuesday, November 12, 2013 6:50 AM