Linq当使用GroupBy时,Include不起作用 [英] Linq when using GroupBy, Include is not working

查看:164
本文介绍了Linq当使用GroupBy时,Include不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包括matchparticipants不起作用。调试时总是说Null。
但是当我把GroupBy发表评论时,工作正常。我正在使用实体框架4.3.1代码优先。



实体:

  public class Match 
{
[ScaffoldColumn(false)]
public int MatchId {get;组; }

[必需(ErrorMessage =Matchtype是必填字段)]
public int Scheme {get;组;

[必需]
[DefaultValue(false)]
public bool完成{get;组; }

public int Round {get;组;


//关系
[必需]
public类别Category {get;组; }

public官方{get;组; }

public Slot Slot {get;组; }

public ICollection< MatchParticipant> MatchParticipants {get;组;


public class MatchParticipant
{
[ScaffoldColumn(false)]
public int MatchParticipantId {get;组; }

public int Points {get;组; }

public int Goals {get;组; }

[必需]
public Match Match {get;组; }

[必需]
public Team Team {get;组;
}

public class Team
{
[ScaffoldColumn(false)]
public int TeamId {get;组; }

[必需(ErrorMessage =名称是必填字段)]
public string Name {get;组; }

[必需(ErrorMessage =玩家人数是必填字段)]
public int NumberOfPlayers {get;组; }

[必需(ErrorMessage =Coach是必填字段)]
public string Coach {get;组; }

[必需(ErrorMessage =电话是必填字段)]
public string Phone {get;组; }

public string CellPhone {get;组; }

public string Fax {get;组; }

[必需(ErrorMessage =电子邮件是必填字段)]
public string Email {get;组; }

[必需(ErrorMessage =地址是必填字段)]
public Address Address {get;组; }

public Pool Pool {get;组; }

[必需(ErrorMessage =类别是必填字段)]
public Category Category {get;组; }

public ICollection< MatchParticipant> matchParticipants {get;组;
}

var matches =
context.matches
.Include(Official)
.Include(Slot)
。$($)
.Include(Category.Tournament)
.Where(m => m.Category.Tournament.TournamentId == tournamentId)
.GroupBy (m = m。类别);

如何使Include工作?

解决方案

Include 要求查询的形状不变。这意味着您的查询必须返回 IQueryable< Match> GroupBy 运算符可能被认为是形状改变,因为它返回 IQueryable< IGrouping< TKey,TSource> 。一旦查询的形状改变,所有的Include语句将被省略。因为您不能使用 Include 与投影,自定义联接和分组。



作为解决方法,您可以执行分组在Linq对象中:

  var matches = context.matches 
.Include(Official)
.Include(Slot)
.Include(MatchParticipants.Team)
.Include(Category.Tournament)
.Where(m => m.Category。 Tournament.TournamentId == tournamentId)
.ToList()
.GroupBy(m => m.Category);


include matchparticipants is not working. It always says Null when I debug. But when I put the GroupBy in comment it works fine. I am using Entity framework 4.3.1 with code-first.

Entities:

public class Match
    {
        [ScaffoldColumn(false)]
        public int MatchId { get; set; }

        [Required(ErrorMessage = "Matchtype is a required field")]
        public int Scheme { get; set; }

        [Required]
        [DefaultValue(false)]
        public bool Finished { get; set; }

        public int Round { get; set; }


        // Relations
        [Required]
        public Category Category { get; set; }

        public Official Official { get; set; }

        public Slot Slot { get; set; }

        public ICollection<MatchParticipant> MatchParticipants { get; set; }
    }

 public class MatchParticipant
    {
        [ScaffoldColumn(false)]
        public int MatchParticipantId { get; set; }

        public int Points { get; set; }

        public int Goals { get; set; }

        [Required]
        public Match Match { get; set; }

        [Required]
        public Team Team { get; set; }
    }

public class Team
    {
        [ScaffoldColumn(false)]
        public int TeamId { get; set; }

        [Required(ErrorMessage="Name is a required field")]
        public string Name { get; set; }

        [Required(ErrorMessage="Number of players is a required field")]
        public int NumberOfPlayers { get; set; }

        [Required(ErrorMessage="Coach is a required field")]
        public string Coach { get; set; }

        [Required(ErrorMessage="Phone is a required field")]
        public string Phone { get; set; }

        public string CellPhone { get; set; }

        public string Fax { get; set; }

        [Required(ErrorMessage="Email is a required field")]
        public string Email { get; set; }

        [Required(ErrorMessage="Address is a required field")]
        public Address Address { get; set; }

        public Pool Pool { get; set; }

        [Required(ErrorMessage = "Category is a required field")]
        public Category Category { get; set; }

        public ICollection<MatchParticipant> matchParticipants { get; set; }
    }

        var matches =
        context.matches
       .Include("Official")
       .Include("Slot")
       .Include("MatchParticipants.Team")
       .Include("Category.Tournament")
       .Where(m => m.Category.Tournament.TournamentId == tournamentId)
       .GroupBy(m => m.Category);

How can I make the Include work?

解决方案

Include demands that the shape of the query doesn't change. It means that your query must return IQueryable<Match>. GroupBy operator is probably considered as shape changing because it returns IQueryable<IGrouping<TKey, TSource>>. Once the shape of the query changes all Include statements are omitted. Because of that you cannot use Include with projections, custom joins and groupings.

As a workaround you can execute grouping in Linq-to-objects:

var matches = context.matches
                     .Include("Official")
                     .Include("Slot")
                     .Include("MatchParticipants.Team")
                     .Include("Category.Tournament")
                     .Where(m => m.Category.Tournament.TournamentId == tournamentId)
                     .ToList()
                     .GroupBy(m => m.Category);

这篇关于Linq当使用GroupBy时,Include不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆