Solved

c# linq Join (i think)

Posted on 2010-08-25
3
312 Views
Last Modified: 2012-05-10
HI,

i am using c# 3.5 and am trying to add to an existing linq query.  But am stuck on how to query a repeating property on a class.

Here are the details:

I working with the following entity structure

public class Office
{
      private int Id{get;set;}
      private List<StaffForYear> StaffForYears {get;set}      

}

public class StaffForYear
{
  public int Id{get;set;}
  public int Year{get;set;}  
  public StaffType StaffType{get;set;}
  public int StaffCount{get;set;}    

}

i am trying to write a linq query which says in psuedo code:

select * offices where Office.StaffForYear.StaffCount was <= startOfMyRange And
                  Office.StaffForYear.StaffCount was <= endOfMyRange      

Finally i want to do this using a "Where" clause on an existing IQueryable<Office> query.
As i am building up a dynamic query.

So something like  

   public IQueryable<Office> CreateStaffCountQuery(int startOfMyRange, int endOfMyRange, IQueryable<Office> query)

   {

     return query.Where(o => o.StaffForYears............);

   }



Thankyou very much.
0
Comment
Question by:MrKevorkian
  • 2
3 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33523097

List<Office> offices = new List<Office>();

int startOfMyRange;
int endOfMyRange;

var query = from o in offices
                        where o.StaffForYears.Any(s => s.StaffCount <= startOfMyRange && s.StaffCount <= endOfMyRange)
                        select o;

Open in new window

0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33523139

Same using Where clause and to get it as IQueryable<Office>

List<Office> offices = new List<Office>();

int startOfMyRange;
int endOfMyRange;

IQueryable<Office> filteredOffices = offices.Where(o => o.StaffForYears.Any(s => s.StaffCount <= startOfMyRange && s.StaffCount <= endOfMyRange)).AsQueryable();

Open in new window

0
 
LVL 1

Author Comment

by:MrKevorkian
ID: 33523224
great! thanks alot!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question