简体   繁体   中英

How to use LINQ on a join table?

The query I am trying to replicate in LINQ is:

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.

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. Something like this (most likely you are speaking for User and Role , but that's not essential):

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:

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:

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 .

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. Take a look at the MSDN documentation over here:

https://msdn.microsoft.com/en-us/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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM