[英]How to use LINQ on a join table?
The query I am trying to replicate in LINQ is: 我试图在LINQ中复制的查询是:
SELECT count(*) FROM joinTable WHERE object1ID = input_parameter1_from_code
AND object2ID = input_parameter2_from_code;
I have access to a IdentityDbContext, but it only contains references to the constituent objects' tables, not for the join table itself, so I don't know what to look for to try to get the result. 我可以访问IdentityDbContext,但是它仅包含对组成对象表的引用,而不是对联接表本身的引用,因此我不知道要查找什么才能尝试获得结果。
Alternatively, if I can just use this raw query, I would like to know how to do that as well. 另外,如果我只能使用这个原始查询,我也想知道如何做。 Thank you.
谢谢。
I assume you have in mind many-to-many
relationship with implicit "link" ("join", "junction") table. 我假设您已经考虑
many-to-many
具有隐式“ link”(“ join”,“ junction”)表many-to-many
关系。 Something like this (most likely you are speaking for User
and Role
, but that's not essential): 这样的事情(很可能您是在讲
User
和Role
,但这不是必需的):
public class One
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Two> Twos { get; set; }
}
public class Two
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<One> Ones { get; set; }
}
public class MyDbContext : DbContext
{
public DbSet<One> Ones { get; set; }
public DbSet<Two> Twos { get; set; }
}
Although you have no direct access to the link table, you can use either of the two "primary" tables combined with the navigation property of the other. 尽管您无法直接访问链接表,但是可以将两个“主”表中的任何一个与另一个的导航属性结合使用。
So, given 所以,给定
var db = new MyDbContext();
both 都
int count =
(from one in db.Ones
from two in one.Twos
where one.Id == input_parameter1_from_code && two.Id == input_parameter2_from_code
select new { one, two })
.Count();
and 和
int count =
(from two in db.Twos
from one in two.Ones
where one.Id == input_parameter1_from_code && two.Id == input_parameter2_from_code
select new { one, two })
.Count();
will produce identical SQL query similar to this: 将产生与以下类似的相同SQL查询:
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[TwoOne] AS [Extent1]
WHERE (1 = [Extent1].[One_Id]) AND (2 = [Extent1].[Two_Id])
) AS [GroupBy1]
which as you can see is against the link table. 如您所见,它与链接表相对。
In query syntax: 在查询语法中:
var amount = (from record in DBcontext.joinTable
where record.object1ID = input_parameter1_from_code &&
record.object2ID = input_parameter2_from_code
select record).Count();
In Method syntax: 在Method语法中:
var amount = DBcontext.joinTable
.Where(record => record.object1ID = input_parameter1_from_code &&
record.object2ID = input_parameter2_from_code)
.Count();
You can use Database.SqlQuery method which accepts raw sql query along with the parameters that you need to use with your query and advantage of using sql parameter
is to avoid sql injection
. 您可以使用Database.SqlQuery方法,该方法接受原始sql查询以及您需要在查询中使用的
sql parameter
,使用sql parameter
好处是可以避免sql injection
。
Try like this: 尝试这样:
var data = yourContext.Database.SqlQuery<int>(
"SELECT count(*) FROM joinTable WHERE object1ID = @code1 AND object2ID = @code2",
new SqlParameter("@code1", input_parameter1_from_code),
new SqlParameter("@code2", input_parameter2_from_code)
);
Let me know if this didnt work for you :) 让我知道这是否对您不起作用:)
You can definitely use that query with a DbContext. 您绝对可以在DbContext中使用该查询。 Take a look at the MSDN documentation over here:
在这里查看MSDN文档:
https://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.executequery(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.data.linq.datacontext.executequery(v=vs.110).aspx
It will be something like: 它将类似于:
var Count = DbContext.ExecuteQuery("SELECT count(*) FROM joinTable where object1ID = input_parameter1_from_code
AND object2ID = input_parameter2_from_code;");
即使链接表也可以使用
dbContext.CollectionOne.where(x => x.Id == 1).SelectMany(x => x.Collection2).where(y => y.Id == 2).Count()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.