locked
How can i filter master data based on details data RRS feed

  • Question


  • Hi..
    I need to obtain all students who obatained more than 70 marks or who are located in EAST...

    How can i write linq query based on the above criteria.


    Students

    ID    Name
     1     ABC
     2     DEF
     3     GHI




    Marks

    ID    Subject  Marks    StudentId
     1     MATH     80            1
     2     PHY        90            2
     3     CHEM     50            3


    LOCATIONS

    ID    NAME   StudentId
     1    EAST       1
     2    SOUTH     2
     3    WEST       3
    Wednesday, April 10, 2013 6:53 PM

Answers

  • partial void StudentsHiMarksByLocation_PreprocessQuery( ref IQueryable<Student> query)
    {
    try
    {
    query = from q in query where q.Marks.Any(M=>M.Marks>70)
    
    select q;
    }
    catch(System.Exception ex)
    {
    throw new Exception(ex.Message");
    }
    
    }

    This worked correctly
    • Marked as answer by m3awadhi Thursday, April 11, 2013 9:28 PM
    Thursday, April 11, 2013 9:28 PM

All replies

  • Can you achieve what you want using a custom query on Student table?

    You can optionally make the Marks value and the location Name parameters to use in the query.

    'StudentsHiMarksByLocation' as a suggested name...

    If you need LINQ then get back, thanks.


    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.

    Wednesday, April 10, 2013 7:11 PM
  • Hi Dave...

    I tried to create custom query on student table and  used Preprocess query to filter my data 

    partial void StudentsHiMarksByLocation_PreprocessQuery( ref IQueryable<Student> query)
    {
    try
    {
    query = from q in query
    let _Marks = from M in q.Marks.Where(MM=>MM.Marks>70)
    select M
    let _Locations = from L in q.Locations.Where(LL=>LL.Name=="EAST") select L
    
    where (_Marks != null)||(_Locations!=null)
    select q;
    }
    catch(System.Exception ex)
    {
    throw new Exception(ex.Message");
    }
    
    }
    But  i get  object reference not set to an instance of an object error ...
    Do you have any clue where am i making mistake ?

    Wednesday, April 10, 2013 8:51 PM
  • partial void StudentsHiMarksByLocation_PreprocessQuery( ref IQueryable<Student> query)
    {
    try
    {
    query = from q in query where q.Marks.Any(M=>M.Marks>70)
    
    select q;
    }
    catch(System.Exception ex)
    {
    throw new Exception(ex.Message");
    }
    
    }

    This worked correctly
    • Marked as answer by m3awadhi Thursday, April 11, 2013 9:28 PM
    Thursday, April 11, 2013 9:28 PM