Dynamic Linq Library can't handling one on many relationship in select clause

asp.net dynamic-linq linq linq-expressions predicatebuilder

Question

I would like to get records from the database using Dynamic Linq Library NuGet. How can I write query for select parent records along with list of child records. For example there is One-on-Many relationship between Question and Answers table. QuestionID is a Foreign Key column in Answers table.

It is very simple if i give column name in where clause when i am not going to use Dynamic Linq Library NuGet.

var LinQResult=db.Questions
            .Include(f=>f.Answers).Where(f=>f.Email=="someEmail").ToList();

I have no question on above query and i can simply render the content on razor views or any web forms.

If i am going to use Dynamic Linq Library NuGet, I have a search criteria in string format like below,

string SearchCreteria = "Email=\"SomeValue\"";

Passing the above dynamic search criteria in where clause below,

var QueryBuilder = (from q in db.Questions
                    join a in db.Answers on q.QuestionID equals a.QuestionID into answer
                    from a in answer.DefaultIfEmpty()                                
                    select new { q,a}).Distinct().AsQueryable();

var Result = QueryBuilder.Where(SearchCreteria);

How can I convert/add above Result into a strongly typed list below

List<QuestionVM> questionVM=new List<QuestionVM> ();
public class QuestionVM
        {
            public Question Question { get; set; }
            public List<Answer> Answers { get; set; }
        }
1
0
4/25/2015 10:33:34 AM

Popular Answer

This should do it I think.

var queryBuilder = (from q in db.Questions.Include(itm  => itm.Answers)
join a in db.Answers on q.QuestionID equals a.QuestionID
where a.Email.equals(SearchCriteria)
select q).Distinct().ToList();

var questionVM = new List<QuestionVM>();
foreach(var q in queryBuilder)
{
   questionVM.add(new QuestionVM
   {
      Question = q,
      Answers = q.Answers.Where(itm => itm != null).Select(itm => itm.QuestionID == q.ID)
   });
}
0
4/25/2015 10:58:44 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow