locked
Query Entities Not Exist in Another Entity RRS feed

  • Question

  • In My Real Estate Application I have Contract and Unit Entities , 

    To make New Contract ,  i need to Select Only Free Units , which are not rented - in a contract - before   , as follow 

    select * from Unit where UnitID not in 
    (
      select distinct UnitID from Contract where ContractStatus ='Active'
    )

    How to make That Lightswitch Query .

    omar awwad


    • Edited by omar_awwad Wednesday, January 9, 2013 3:44 PM
    Wednesday, January 9, 2013 3:44 PM

Answers

  • again i found the solution , 

            partial void SearchUnits_PreprocessQuery(int? Para_BuildID, int? Para_AreaID, int? Para_GOVID, ref IQueryable<Unit> query)
            {
                query = from u in query where u.Contracts.Any(c => c.Status == 2) ||
                         u.Contracts.Count() == 0 select u;
            }

    thanks Yann For your help with concept Any() , my example is simple than the post you mention as Contract ant Unit Are already entity objects in my application , 

    this code get all units which have no Contracts , or Contracts with Terminated status  [2]

    omar awwad

    • Marked as answer by omar_awwad Thursday, January 10, 2013 4:07 PM
    Thursday, January 10, 2013 4:07 PM

All replies

  • The technique for using the .Any() method, that I describe in Is there anyway to search on table values that do not have FKs in PreprocessQuery? is what you need here.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, January 10, 2013 4:23 AM
    Moderator
  • again i found the solution , 

            partial void SearchUnits_PreprocessQuery(int? Para_BuildID, int? Para_AreaID, int? Para_GOVID, ref IQueryable<Unit> query)
            {
                query = from u in query where u.Contracts.Any(c => c.Status == 2) ||
                         u.Contracts.Count() == 0 select u;
            }

    thanks Yann For your help with concept Any() , my example is simple than the post you mention as Contract ant Unit Are already entity objects in my application , 

    this code get all units which have no Contracts , or Contracts with Terminated status  [2]

    omar awwad

    • Marked as answer by omar_awwad Thursday, January 10, 2013 4:07 PM
    Thursday, January 10, 2013 4:07 PM
  • "thanks Yann For your help with concept Any()"

    You're welcome.

    "my example is simple than the post you mention"

    Yeah, sorry about that. I just didn't have time to rewrite a more specific example. I hoped you'd pick up what you needed from the post anyway, which you did.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Friday, January 11, 2013 4:41 AM
    Moderator