c# - 比较两个表之间的属性
问题描述
我正在尝试做一些非常简单的事情,但由于某种原因我有点纠结。我有两个表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
}
}
}
}
}
}
我对它很陌生,我想这是菜鸟问题,我仍然很高兴得到你的帮助。谢谢参考,谢谢
解决方案
如果您确定下面的 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
}
}
推荐阅读
- javascript - Javascript 字符串拆分 |
- mysql - 无法创建/写入文件'/path/file.csv'(错误代码:13“权限被拒绝”)
- regex - 正则表达式只允许某些字符
- sql - 从字符串中删除特殊字符 +
- ruby-on-rails - 审核 gem 以在管理面板中列出跟踪的更改
- python - Python 性能时间减去
- python - 在 Python 中使用字典时,如何返回具有相同值的键?
- c - gcc 错误 - 可能是库
- facebook - 带有移动应用程序和后端 Web 服务器的 Facebook OAuth
- javascript - 如何在 Liferay 中将“destoryOnHide”或“destroyOnClose”设置为现有的模态对话框?