简体   繁体   English

EntityFramework按ID过滤列表

[英]EntityFramework Filter a list by id

I have this function: 我有这个功能:

[HttpPost]
[Route("")]
/// <summary>
/// Create a team
/// </summary>
/// <param name="model">The team model</param>
/// <returns>The modified team model</returns>
public async Task<IHttpActionResult> Create(TeamBindingViewModel model)
{

    // If our model is invalid, return the errors
    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    // Get all our colours
    var colours = await this.colourService.GetAllAsync();

    // Create our new model
    var team = new Team()
    {
        Name = model.Name,
        Sport = model.Sport
    };

    // For each colour, Add to our team
    team.Colours = colours.Join(model.Colours, c => c.Id, m => m.Id, (c, m) => new Colour { Id = c.Id, Hex = c.Hex, Name = c.Name }).ToList();

    // Create our team
    this.service.Create(team);

    // Save our changes
    await this.unitOfWork.SaveChangesAsync();

    // Assign our Id to our model
    model.Id = team.Id;

    // Return Ok
    return Ok(model);
}

If I run it, then EntityFramework enters new colours into the database rather than referencing the colours. 如果我运行它,那么EntityFramework会将新颜色输入数据库,而不是引用颜色。 I know this is because I am creating a new Colour in my join. 我知道这是因为我正在加入联接中创建新的颜色 If I change my function to this: 如果我将功能更改为此:

[HttpPost]
[Route("")]
/// <summary>
/// Create a team
/// </summary>
/// <param name="model">The team model</param>
/// <returns>The modified team model</returns>
public async Task<IHttpActionResult> Create(TeamBindingViewModel model)
{

    // If our model is invalid, return the errors
    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    // Get all our colours
    var colours = await this.colourService.GetAllAsync();

    // Create our new model
    var team = new Team()
    {
        Name = model.Name,
        Sport = model.Sport
    };

    // For each colour, Add to our team
    team.Colours = colours;

    // Create our team
    this.service.Create(team);

    // Save our changes
    await this.unitOfWork.SaveChangesAsync();

    // Assign our Id to our model
    model.Id = team.Id;

    // Return Ok
    return Ok(model);
}

Everything works fine. 一切正常。 The only thing that is changed in the second snippet is : 在第二个片段中唯一更改的是:

team.Colours = colours.Join(model.Colours, c => c.Id, m => m.Id, (c, m) => new Colour { Id = c.Id, Hex = c.Hex, Name = c.Name }).ToList();

became 成为

team.Colours = colours;

which is the list retrieved from the database. 这是从数据库中检索到的列表。 EntityFramework knows that this has not been changed so it just references the colours instead of creating new ones. EntityFramework知道此更改尚未更改,因此仅引用颜色而不创建新的颜色。 How can I get my filtered list to do the same? 我怎样才能让我的过滤列表做同样的事情?

Cheers, /r3plica 干杯,/ r3plica

Ok, I knew it was simple. 好吧,我知道这很简单。 I changed the problem line to this: 我将问题行更改为:

// For each colour, Add to our team
team.Colours = colours.Where(m => model.Colours.Any(c => c.Id == m.Id)).ToList();

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

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