Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表中的LINQ查询列表_C#_Linq - Fatal编程技术网

C# 列表中的LINQ查询列表

C# 列表中的LINQ查询列表,c#,linq,C#,Linq,我有以下EF对象列表,联盟,其中每个都有一个团队的列表。每个团队都有一年与其关联 在WPF表单中,我有一个组合框可以选择多年。当我选择一年时,我希望能够按照当年存在的联赛进行排序,然后是它拥有的球队,这样我就可以为它填充一个TreeView 到目前为止,我了解的最多的是: public int SelectedYear { get => _SelectedYear; set { _SelectedYea

我有以下EF对象列表,
联盟
,其中每个都有一个
团队的列表
。每个
团队
都有一年与其关联

在WPF表单中,我有一个
组合框
可以选择多年。当我选择一年时,我希望能够按照当年存在的联赛进行排序,然后是它拥有的球队,这样我就可以为它填充一个
TreeView

到目前为止,我了解的最多的是:

    public int SelectedYear
    {
        get => _SelectedYear;
        set
        {
            _SelectedYear = value;
            RaisePropertyChangedEvent("SelectedYear");
            Refresh();
        }
    }

    public void Refresh(object sender = null, EventArgs e = null)
    {
        Leagues = new ObservableCollection<League>(_Locator.Statistix.Leagues.Where(x => x.Teams.Count(t => t.Year == SelectedYear) > 0).ToList());
        foreach(League l in Leagues)
        {
            l.Teams.Where(x => x.Year == SelectedYear);
        }
    }
为了更好地衡量,这里还有
团队
对象:

public partial class Team
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Team()
    {
        this.Games = new HashSet<Game>();
        this.Games1 = new HashSet<Game>();
    }

    public int ID { get; set; }
    public int FranchiseID { get; set; }
    public int Year { get; set; }
    public Nullable<int> CityID { get; set; }
    public string Nickname { get; set; }
    public Nullable<int> BallparkID { get; set; }
    public int LeagueID { get; set; }

    public virtual City City { get; set; }
    public virtual Franchise Franchise { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Game> Games { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Game> Games1 { get; set; }
    public virtual League League { get; set; }
}
公共部分类团队
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共团队()
{
this.Games=新HashSet();
this.Games1=新的HashSet();
}
公共int ID{get;set;}
公共ID{get;set;}
公共整数年{get;set;}
公共可空CityID{get;set;}
公共字符串昵称{get;set;}
公共可为空的BallparkID{get;set;}
public int LeagueID{get;set;}
公共虚拟城市城市{get;set;}
公共虚拟特许经营权{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection游戏{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection游戏1{get;set;}
公共虚拟联盟{get;set;}
}

是否有一种方法可以过滤同一
联盟中的团队所遵循的联盟?
可观察的集合

如果联盟的规模不是太大,我会研究收集数据,然后取出您不需要的数据

public void Refresh(object sender = null, EventArgs e = null)
    {
        Leagues = new ObservableCollection<League>(_Locator.Statistix.Leagues.ToList());
        foreach(League l in Leagues)
        {
            l.Teams.RemoveAll(x => x.Year == SelectedYear);
        }
        Leagues.RemoveAll(x => x.Teams.Count == 0);
    }
public void刷新(object sender=null,EventArgs e=null)
{
Leagues=新的ObservableCollection(_Locator.Statistix.Leagues.ToList());
foreach(联赛中的l联赛)
{
l、 Teams.RemoveAll(x=>x.Year==SelectedYear);
}
Leagues.RemoveAll(x=>x.Teams.Count==0);
}

另一种选择是构建从ObserableCollection中固有并实现IObserable的专用类,以构建专用逻辑,将这些类用作WPF表单的datacontext,从而允许系统在选择不同年份时自动过滤。

将许多列表作为一个整体展开您可以使用的单一列表:


那会有用的,除了我想保持它在树上的层次性我知道你要去哪里,不知道最好的方法是什么,但我发现了这个线索,我希望它能帮助你:你在使用足球数据API吗?我有一个回购协议,虽然我没有使用WPF,也许它可以帮助你!你能给我们看一下联盟对象吗?@YairHalberstadt我刚刚更新了我的问题,提供了
League
Team
对象。这一个可能有效,尽管因为它是EF对象,我得到了一个
LINQ to Entities
错误。谢谢
public void Refresh(object sender = null, EventArgs e = null)
    {
        Leagues = new ObservableCollection<League>(_Locator.Statistix.Leagues.ToList());
        foreach(League l in Leagues)
        {
            l.Teams.RemoveAll(x => x.Year == SelectedYear);
        }
        Leagues.RemoveAll(x => x.Teams.Count == 0);
    }
// all teams from all leagues
var allTeams = GetAllLeagues().SelectMany(l => l.Teams);

// all teams of the SelectedYear
var teamsByYear = allTeams.Where(t => t.Year == SelectedYear);
public void Refresh(object sender = null, EventArgs e = null)
{
    Leagues = _Locator.Statistix.Leagues.Select(x => new League
        {
             Id = x.Id,
             Name = x.Name,
             Initials = x.Initials,
             LahmanId = x.LahmanId,
             Teams = x.Teams.Where(x => x.Year == SelectedYear).ToList()
        }).Where(x=> x.Teams.Count > 0).ToList();
}