Linq to Entity Filter使用导航属性

时间:2018-01-05 16:58:09

标签: c# linq entity-framework-core

我想在Entity Framework中过滤导航属性。从这里给出的例子https://docs.microsoft.com/en-us/ef/core/querying/related-data

import sys
import pygame as pg
from pygame.math import Vector2

width = 1280
height = 720
x1 = 200
y1 = 100
x2 = 500
y2 = 400
x3 = 100
y3 = 300
x = 0
y = 0

class Player(pg.sprite.Sprite):

    def __init__(self, pos, *groups):
        super().__init__(*groups)
        pg.sprite.Sprite.__init__(self)
        self.image = pg.image.load("character.png")
        self.rect = self.image.get_rect(center=pos)
        self.vel = Vector2(0, 0)
        self.pos = Vector2(pos)

    def update(self):
        self.pos += self.vel
        self.rect.center = self.pos

#enemy class
class Enemy(pg.sprite.Sprite):

    def __init__(self, pos, waypoints, *groups):
        super().__init__(*groups)
        self.image = pg.image.load("enemy.png")
        self.image = pg.transform.scale(self.image, (int(50), int(50)))
        self.rect = self.image.get_rect(center=pos)
        self.vel = Vector2(0,0)
        self.max_speed = 5
        self.pos = Vector2(pos)
        self.waypoints = waypoints
        self.waypoint_index = 0
        self.target = self.waypoints[self.waypoint_index]
        self.target_radius = 50
        self.rect.x = width / 2
        self.rect.y = height / 2

    def update(self):
# A vector pointing from self to the target.
        heading = self.target - self.pos
        distance = heading.length()  # Distance to the target.
        heading.normalize_ip()
        if distance <= 2:  # We're closer than 2 pixels.
          # Increment the waypoint index to swtich the target.
          # The modulo sets the index back to 0 if it's equal to the length.
          self.waypoint_index = (self.waypoint_index + 1) % len(self.waypoints)
          self.target = self.waypoints[self.waypoint_index]
        if distance <= self.target_radius:
                # If we're approaching the target, we slow down.
          self.vel = heading
        else:  # Otherwise move with max_speed.
          self.vel = heading * self.max_speed

        self.pos += self.vel
        self.rect.center = self.pos

#Enemy waypoints
waypoints = [[x1, y1], [x2, y2], [x3, y3]]


class Floor(pg.sprite.Sprite):

    def __init__(self, x, y, *groups):
        super().__init__(*groups)
        self.image = pg.image.load("floor.png")
        self.rect = self.image.get_rect(topleft=(x, y))
        self.rect.x = x
        self.rect.y = y

class SideWall(pg.sprite.Sprite):

    def __init__(self, x, y, *groups):
        super().__init__(*groups)
        self.image = pg.image.load("sidewall.png")
        self.rect = self.image.get_rect(topleft=(x, y))
        self.rect.x = x
        self.rect.y = y

class TopAndBottomWall(pg.sprite.Sprite):

    def __init__(self, x, y, *groups):
        super().__init__(*groups)
        self.image = pg.image.load("topandbottomwall.png")
        self.rect = self.image.get_rect(topleft=(x, y))
        self.rect.x = x
        self.rect.y = y


def main():
    screen = pg.display.set_mode((1280, 720))
    clock = pg.time.Clock()
    #all the sprites group
    all_sprites = pg.sprite.Group()

    #the floor
    floor = Floor(540, -620, all_sprites)

    #player
    player = Player(((width / 2), (height / 2)), all_sprites)

    #walls group
    walls = pg.sprite.Group()

    #all walls
    walltop = TopAndBottomWall(540, -620, all_sprites, walls)
    wallbottom = TopAndBottomWall(540, 410, all_sprites, walls)
    wallleft = SideWall((width / 2) - 100, (height / 2) - 930, all_sprites, walls)
    wallright = SideWall((wallleft.rect.x + (1920 - 50)), (height / 2) - 930, all_sprites, walls)

    #all enemy's
    enemy = Enemy((100, 300), waypoints, all_sprites)

    camera = Vector2(0, 0)

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            #player movement    
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_d:
                    player.vel.x = 5
                elif event.key == pg.K_a:
                    player.vel.x = -5
                elif event.key == pg.K_w:
                    player.vel.y = -5
                elif event.key == pg.K_s:
                    player.vel.y = 5
            elif event.type == pg.KEYUP:
                if event.key == pg.K_d and player.vel.x > 0:
                    player.vel.x = 0
                elif event.key == pg.K_a and player.vel.x < 0:
                    player.vel.x = 0
                elif event.key == pg.K_w:
                    player.vel.y = 0
                elif event.key == pg.K_s:
                    player.vel.y = 0

        camera -= player.vel

        all_sprites.update()

        if pg.sprite.spritecollide(player, walls, False):
            #stop the left wall from moving
            wallleft.rect.x = wallleft.rect.x + player.vel.x
            wallleft.rect.y = wallleft.rect.y + player.vel.y
            #stop the top wall from moving
            walltop.rect.y = walltop.rect.y + player.vel.y
            walltop.rect.x = walltop.rect.x + player.vel.x
            #stop the right wall from moving
            wallright.rect.x = wallright.rect.x + player.vel.x
            wallright.rect.y = wallright.rect.y + player.vel.y
            #stop the bottom wall from moving
            wallbottom.rect.x = wallbottom.rect.x + player.vel.x
            wallbottom.rect.y = wallbottom.rect.y + player.vel.y
            #stop the floor from moving
            floor.rect.x = floor.rect.x + player.vel.x
            floor.rect.y = floor.rect.y + player.vel.y


        screen.fill((0, 0, 0))

        for sprite in all_sprites:
            screen.blit(sprite.image, sprite.rect.topleft+camera)

        pg.display.flip()
        clock.tick(30)

main()
pg.quit()

如果我想过滤特定博客和特定帖子,我该如何过滤?即使帖子不存在,也必须返回博客详细信息

我想要一些像下面的东西

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .ToList();
}

在上述情况下,如果postid 2可用于blogid 1则应返回两者。如果博客可用,则应仅返回博客详细信息。我怎样才能做到这一点?

从SQL的角度来看,它就像一个左连接。

此致 乔

1 个答案:

答案 0 :(得分:0)

两个查询的群组联接应该有效(以下假设您希望每个博客发布一个帖子,如果不是这样,请删除FirstOrDefault()):

var blogs = context.Blogs.Where(a => a.BlogId == 1);
var posts = context.Posts.Where(a => a.PostId == 2);


var blogWithPost = blogs.GroupJoin(posts, a => a.BlogId, a => a.PostId, (a,b) => new { Blog=a, Post = b.FirstOrDefault() });