1

I am using System.Linq.Dynamic library. Is there a way to use Entities with one to many relationship. To be specific I have 3 classes

class A
{
    public int Id {get;set;}
    ...
    ...

    public Class2 class2 {get;set;}

    public ICollection<Another> Bs{get;set;}
}

class Class2
{
    public int Id{get;set;}
}
class Another
{
    public int Id {get;set;}
}

I can filter when the relationship is one to one

public IQueryable<T> Select<T>(string condition,object value)
{
    var list=FindAll<T>();//return list of T from database
    var result=list.Where(string.Format("{0} = @0",condition),value);
}

result=Select<A>("class2.Id",1);

the above call gives me the list of A objects whose class2's id=1. how can i search if the relationship is one to many.. how do i filter list of A objects based on each object's first item of Bs ie Bs[0]. would it be possible to filter using Linq.Dynamic

2 Answers 2

1

DynamicQueryable.cs has IEnumerableSignatures interface

This interface has

 void Any(bool predicate);

using this method we could Filter the data

 var reuslt=list.Where("Bs.Any(Id =@0)",value);

this will do the filtering of data.

0

I haven't tested your code, but if it works for class2.Id, then the following should work for the Id of the first item in Bs:-

result=Select<A>("Bs[0].ElementAt(0)",1);

If Bs could be empty, then you could use

result=Select<A>("Bs[0].ElementAtOrDefault(0)",1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.