【问题标题】:finding record in look up table using linq使用 linq 在查找表中查找记录
【发布时间】:2012-08-15 12:22:27
【问题描述】:

我开始掌握 LINQ,但我一直停留在将查找表实现到对象中然后查询该对象(或它的关联)的基本概念和技术上。这可能是一个愚蠢的问题,并且可能有一个我应该自己解决的答案。但我还没有找到一个解释,使这个方法坚持下去。

所以我创建了一个这样的示例数据库结构

我想要两个 LINQ 查询,第一个提供所有 example2 记录,这些记录通过查找指定的 object1 和第二个提供我所有与指定 object2

相关的 example1 记录

希望有人可以启动我的大脑。

类似

var examples = (from e in db.examples where e.example2.id == id).ToList();

快速编写的 SQL 查询

SELECT * FROM [dbo].[example1] one
JOIN [dbo].[examplelookup] lu ON one.[id] = lu.[example1id]
JOIN [dbo].[example2] two ON lu.[example2id] = two.[id]
WHERE one.[id] = 1

刚刚结束并创建了这个,我认为应该解释一下

【问题讨论】:

  • 首先,没有愚蠢的问题。其次,请提供一些代码:)
  • example2 和 example1 是否共享相同的键值?
  • 为什么不从如何向数据库中的数据提出问题开始——忽略 LINQ,只考虑 SQL 语言,然后从那里开始工作。

标签: c# linq orm telerik


【解决方案1】:

我知道你接受了一个答案,但只是为了记录:

这是使用Any() 的常见场景,它在sql 中转换为exists。当您希望 example1s 引用特定的 example2(由 id 指定)时,请执行以下操作:

from ex1 in example1
where ex1.example2s.Any(e => e.id = id)
select ex1

这为您提供了一个不同的 example1 对象列表,而带有 SelectMany 的解决方案可能会生成一个包含重复项的列表。

【讨论】:

  • 我已将 Any 解决方案纳入我的答案并给予您信任。另外修改了你。奇怪,我通常会先想到任何解决方案,我今天一定要休息一天......
  • 感谢 Gert 也得分了
【解决方案2】:

假设

example1-> examplelookup 关系名称是 examplelookups

example2-> examplelookup 关系名称是 examplelookups

examplelookup -> example1 关系名称是 example1

examplelookup -> example2 关系名称是 example2

给定id of example1

db.example1.Where(x=>x.id == id).SelectMany(x=>x.examplelookups).Select(x=>x.example2);

反之亦然

db.example2.Where(x=>x.id == id).SelectMany(x=>x.examplelookups).Select(x=>x.example1);

编辑:基于 Gert Arnold 建议的使用 any 子句的答案的附加更新(这是在已被接受为答案之后)

db.example1.Where(x=>x.examplelookups.Any(y=>y.example2.id == id));

反之亦然

db.example2.Where(x=>x.examplelookups.Any(y=>y.example1.id == id));

第二次编辑(在已经接受并回答但问题被修改以包含数据模型之后)

db.Example1.Where(x=>x.Id == id).SelectMany(x=>x.Example2); // could have duplicates
db.Example2.Where(x=>x.Example1.Any(y=>y.Id == id)); //alternate any form removing the duplicates

反之亦然

db.Example2.Where(x=>x.Id == id).SelectMany(x=>x.Example1); // could have duplicates
db.Example1.Where(x=>x.Example2.Any(y=>y.Id == id)); //alternate any form removing the duplicates

【讨论】:

    【解决方案3】:

    我相信您正在寻找的是连接。此代码将通过连接表为您提供具有相应 example2 记录的所有 example1 记录:

    var x = from ex1 in example1
            join exlookup in examplelookup on ex1.id equals exlookup.example1id
            join ex2 in example2 on exlookup.example2id equals ex2.id
            select ex1;
    

    相反,ex2 记录,只需将 select ex1 替换为 select ex2

    【讨论】:

    • 仅供参考:该代码未输入到 Visual Studio 中,因此可能会出现错误,但您应该明白这一点。
    • 是的,我一直在寻找类似的东西,但我的域模型将我的查找表作为关联,现在无法像这样访问,(我认为)
    【解决方案4】:

    时间很短,但尝试从查找表开始,这样您就可以查询两个关联

    var examples = (from el in db.examplelookup 
                    where el.example2id == id
                    select el.examples1.ToList();
    

    这里也没有进行代码检查。

    【讨论】:

    • 感谢@pleun,但我无法像这样访问 llokup 表我添加了模型的图像来尝试说明我的模型目前的样子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多