使用where子句将linq值(int)与int List进行比较 [英] Compare a linq value (int) to an int List using where clause

查看:174
本文介绍了使用where子句将linq值(int)与int List进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个linq查询,它连接几个表并将值返回到一个对象中。查询工作正常,直到我添加了一个where子句.Aftre where子句,我的查询返回null。



这是代码:



I have a linq query which joins a couple of tables and returns the value into an object. The query was working fine, till i added a where clause to in. Aftre the where clause, my query returns null.

Here's the code:

List<Int32> resourceSupervisorIdList = new List<Int32>();

resourceSupervisorIdList.Add(searchCriteriaTimesheet.ResourceId);

        foreach (resource res in allSubordinateResources)
        {

            if (!resourceSupervisorIdList.Contains(res.id_resource))
                resourceSupervisorIdList.Add(res.id_resource);
        }


using (tapEntities te = new tapEntities())
            {

                var timesheetAll = (from tsh in te.timesheet_header
                                    join rs in te.resources on tsh.id_resource equals rs.id_resource
                                    join tsd in te.timesheet_detail on tsh.id_timesheet equals tsd.id_timesheet
                                    where (resourceSupervisorIdList.Contains(rs.id_resource_supervisor))
                                    select new TimesheetHeaderDetailsItem()
                                    {

                                        OrganizationId = rs.id_organization,
                                        ProjectId = tsd.id_project,
                                        StartDate = tsh.dte_period_start,
                                        EndDate = tsh.dte_period_end,
                                        ApprovedDate = tsh.dte_approved,
                                        RejectedDate = tsh.dte_rejected,
                                        SubmittedDate = tsh.dte_submitted,

                                    });

if (timesheetAll == null || timesheetAll.Count() == 0)
                {

                    return result;
                }
}



现在,在添加where子句后,代码将运行到if条件。表中有匹配的记录,但我仍然无法获得任何记录。




Now, after adding the where clause, the code runs into the if condition. There are matching records in the table, but still i'm not able to get any records.

rs.id_resource_supervisor



在mysql db中是int类型。


is of type int in the mysql db.

推荐答案

请调试你的代码并检查是否,

1. resourceSupervisorIdList填充了您需要的数字。

2. rs确实包含一些常用数字。
Please debug your code and check if,
1. resourceSupervisorIdList is populated with numbers you need.
2. rs does contain some common numbers.


Hi
试试这个,



我正在使用lamda表达式。



Hi try this,

i am using lamda expression.

var result = timesheet_header.Join(resources, (k => k.id_resource), (k => k.id_resource), (tsh, res) => new { tsh, res }).Join(timesheet_detail,
              (k => k.tsh.id_timesheet), (k => k.id_timesheet), (tshres, tsd) => new { tshres, tsd }).Where(k => resourceSupervisorIdList.Contains(k.tshres.res.id_resource_supervisor)).Select(k =>
                  new TimesheetHeaderDetailsItem()
                             {

                                 OrganizationId = k.tshres.res.id_organization,
                                 ProjectId = k.tsd.id_project,
                                 StartDate = k.tshres.tsh.dte_period_start,
                                 EndDate = k.tshres.tsh.dte_period_end,
                                 ApprovedDate = k.tshres.tsh.dte_approved,
                                 RejectedDate = k.tshres.tsh.dte_rejected,
                                 SubmittedDate = k.tshres.tsh.dte_submitted,

                             });


好的,我明白了。我的查询出现问题。我没有拿到正确的身份证。对不起,伙计们,谢谢你的帮助。
Ok, i got it. There was a problem with my query. I wasn't fetching the correct id. Sorry guys and thanks for helping.


这篇关于使用where子句将linq值(int)与int List进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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