Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: LINQ MVC3
Hi
I need to fetech
One Staff from Table:Staff where Staff.StaffID=2
and All respective Item from Table:TimeSheet where TimeSheet.tsDate < Today
 
I was using the following
--------------------------------------------------------------
var foo = (TSDataBase.Staff.Where(p => p.TimeSheet.All(c =>c.tsDate<datetime.today))).single(g> g.StaffID == EmpID);
------------------------------------------------------------
 
It one returning Satff-data but respective Timesheet is null though it is there.
 
Classes/ Relatios are defined for this tables..
 
Can anyone help me?
Posted 1-Sep-12 23:13pm
Edited 2-Sep-12 0:32am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try This one:
var TheMember = from member in TSDataBase.Staff
                where member.StaffID == EmpID
                select new
                {
                    Name = member.Name,
                    Position = member.Position,
                    // and so on based on your Staff table
                    TimeSheetData = from sheet in TSDataBase.TimeSheet
                                where sheet.StaffID = member.StaffID
                                and sheet.tsDate<datetime.today
                                select sheet;
                };
The first part selects the staff data. As I don't know about the members of there two classes, I made my assumptions. The second part (like a inner select) will fill the TimeSheetData member of the newly created anonymous class (you should create a dedicated class for that) will be filled with the timesheet "records" related to that staff member according to the condition.
 
Update:
By the way your question and your fluent api query are confusing. You tell about tables, but you select from an embedded IQueryable. Let's asume used EF DatabaseFirst, and you have navigation on place. Than the above query will look like this:
 
var TheMember = from member in TSDataBase.Staff
                where member.StaffID == EmpID
                select new Staff
                {
                    Name = member.Name,
                    Position = member.Position,
                    TimeSheet = from sheet in member.TimeSheet
                                where heet.tsDate&lt;datetime.today
                                select sheet;
                };
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 2 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100