首页 > 解决方案 > 比较两个表之间的属性

问题描述

我正在尝试做一些非常简单的事情,但由于某种原因我有点纠结。我有两个表userTbl并按列productivityTbl连接userCode

目标是遍历productivityTbl并在其中遍历userTbl以获取用户工作代码的变量(在用户表中有UserPermission描述用户工作编号的变量),然后对收到的角色编号执行操作。

问题是,代码不知道如何将对象转换为表的类型。我试图提前让对象属于表格类型,但这也无济于事。

我敢肯定,我可能还没有探索所有的方法,但很想获得有关该主题的指导。

这是我的代码:(我删除了不相关的内容)

public static List<object> getTotalWorkPerMonth(int nextMonth)
{
    using (Model2 db = new Model2())
    {
        DateTime today = DateTime.Now;
        DateTime sixMonthsBack = today.AddMonths(-6);

        while (sixMonthsBack < today)
        {
            foreach (var item in db.ProductivityTbl.ToList())
            {
                if (item.Date.Value.Month == sixMonthsBack.Month)
                {
                    // Here's my problem, when the userRoll is of type var then the code
                    // gets the header line but he does not let 
                    // me do in switch user.UserPermission and when the userRoll is 
                    // of type userTbl then he requires to do a conversion 
                    // from LINQ to userTbl and does not get the header line.

                    var userRoll = db.UserTbl.Where(u => u.UserCode == item.UserCode);

                    switch (userRoll.UserPermission)
                    {
                        // some code
                    }
                }
            }    
        }
    }
}

我对它很陌生,我想这是菜鸟问题,我仍然很高兴得到你的帮助。谢谢参考,谢谢

标签: c#

解决方案


如果您确定下面的 LINQ 语句会给您一个唯一的记录:

var userRoll = db.UserTbl.Where(u => u.UserCode == item.UserCode);

然后你可能想尝试这样的事情

var userRoll = db.UserTbl.Where(u => u.UserCode == item.UserCode).FirstOrDefault();
if (userRoll != null)
{
    switch (userRoll.UserPermission)
    {
        // some code
    }
}

推荐阅读