使用LINQ从MainTable/ChildTable中获取数据 [英] Fetch Data from MainTable/ChildTable Using LINQ

查看:72
本文介绍了使用LINQ从MainTable/ChildTable中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我需要fetech
表中的一名职员:职员,其中Staff.StaffID = 2
以及Table:TimeSheet中的所有相应项,其中TimeSheet.tsDate<今天

我正在使用以下
-------------------------------------------------- ------------

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);


-------------------------------------------------- ----------

它返回一个Satff数据,但相应的时间表虽然为空,但仍为空.

为此表定义了类/相对论.
有人可以帮我吗?


------------------------------------------------------------

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?

推荐答案

尝试以下一项:
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;
                };


第一部分选择人员数据.因为我不知道那两个班的成员,所以我做了假设.第二部分(如内部选择)将填充新创建的匿名类的TimeSheetData成员(您应为其创建专用的类),并根据条件填充与该工作人员相关的时间表记录". br/>
更新:
顺便说一下,您的问题和流利的api查询令人困惑.您讲述表,但是从嵌入式IQueryable中选择.假设使用了EF DatabaseFirst,您就可以进行导航了.比上面的查询看起来像这样:


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;
                };


这篇关于使用LINQ从MainTable/ChildTable中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆