简体   繁体   English

Linq 和 Lambda - 如何限制连接的表行?

[英]Linq with Lambda - how do I restrict joined table rows?

I want to use Linq to duplicate this T-SQL query on a sports teams database, to look up the experienced players in handball teams:我想使用 Linq 在运动队数据库上复制此 T-SQL 查询,以查找手球队中有经验的球员:

Select TE.TeamName, PL.FirstName, PL.LastName 
From T_Team as TE 
Inner Join T_Player As PL 
On PL.Team_ID = TE.Team_ID 
And PL.ExpLevel = 'Experienced' 
Where TE.SportName = 'Handball'

I've tried creating two entities for my two tables:我尝试为我的两个表创建两个实体:

public class TTeam 
{
    public int TeamId { get; set; }
    public string TeamName { get; set; }
    public string SportName { get; set; }

    public virtual List<TPlayer> TeamPlayers { get; set; }

    // Called in the context OnModelCreating() method
    public static void CreateModel(EntityTypeBuilder<TTeam> p_ebpTable)
    {
        p_etbTable.ToTable("T_TEAM");

        p_etbTable.HasKey(t => new { t.TeamId }).HasName("PK_TEAMID_T_TEAM");

        // Column definitions

        // Foreign Keys
        p_etbTable.HasMany(t => t.TeamPlayers).
                   WithOne(p => p.CurrentTeam).
                   HasPrincipalKey(t => t.TeamId).
                   HasForeignKey(p => p.TeamId);
    }
}

and

public class TPlayer
{
    public int PlayerId { get; set; }
    public int TeamId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ExpLevel { get; set; }

    public virtual TTeam CurrentTeam { get; set; }

    // Called in the context OnModelCreating() method
    public static void CreateModel(EntityTypeBuilder<TPlayer> p_ebpTable)
    {
        p_etbTable.ToTable("T_PLAYER");

        p_etbTable.HasKey(t => new { t.PlayerId }).HasName("PK_PLAYERID_T_PLAYER");

        // Column definitions

        // Foreign Keys
        p_etbTable.HasOne(p => p.CurrentTeam).
                   WithMany(t => t.TeamPlayers).
                   HasForeignKey(p => p.TeamId).
                   HasPrincipalKey(t => t.TeamId);
    }
}

then use them in然后使用它们

using Microsoft.EntityFrameworkCore;

IEnumerable<TTeam> z_enbHandballTeams = z_dbcDbContext.TTeamRepository
    .Where(te => te.SportName == "Handball")
    .Include(te => te.TeamPlayers.Where(pl => pl.ExpLevel == "Experienced"));

but looping through z_enbHandballTeams in a foreach , throws an InvalidOperationException with the message "Lambda expression used inside Include is not valid".但是在foreach中循环z_enbHandballTeams会抛出InvalidOperationException并显示消息“Include 中使用的 Lambda 表达式无效”。

(I guess it goes without saying that ExpLevel is a number and SportName is actually SportId , but I felt it would look easier to read that way.) (我想不用说ExpLevel是一个数字,而SportName实际上是SportId ,但我觉得这样看起来更容易阅读。)

What am I doing wrong?我究竟做错了什么?

EF Core 3.1.x do not support filtered Include . EF Core 3.1.x 不支持过滤的Include Workaround is to do that via Select解决方法是通过Select做到这一点

var z_enbHandballTeams = z_dbcDbContext.TTeamRepository
    .Where(te => te.SportName == "Handball")
    .Select(te => new TTeam
    {
        TeamId = te.TeamId,
        TeamName = te.TeamName,
        SportName = te.SportName,

        TeamPlayers = te.TeamPlayers.Where(pl => pl.ExpLevel == "Experienced")
            .ToList()
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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