gpt4 book ai didi

mysql - 使用 Entity Framework 获取唯一行

转载 作者:行者123 更新时间:2023-11-30 01:19:08 27 4
gpt4 key购买 nike

我使用 3 个表来设置用户角色。

1。模块

ID、姓名

2。行动

Id、Name、ModuleId(带模块的外键)

3。用户操作

Id,UserId,ActionId(带有操作的外键)

我想从 userActions 表中获取用户的唯一模块列表。我正在使用 Entity Framework ,我的数据库是Mysql我使用了查询

var result = (from p in my_accountEntities.useractions
where p.UserId == item.Id
select p.action.module).ToList();

List<module> modules = new List<module>();
if (result != null)
{
modules = (List<module>)result;

}

它不返回唯一列表,而是返回 Useraction 表中的所有行。我如何获得唯一的模块列表(基于 moduleId)

最佳答案

尝试使用 linq .Distinct() 扩展方法

var result = (from p in my_accountEntities.useractions
where p.UserId == item.Id
select p.action.module)
.Distinct().ToList();

关于mysql - 使用 Entity Framework 获取唯一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18755200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com