Solved

c# linq Join (i think)

Posted on 2010-08-25
3
286 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now