本文介绍了如果找不到玩家,则无法找到默认查询为0的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如果找不到防御性球员,我找不到默认的等级为0的方法.
我尝试创建防御性球员的列表,如果该列表返回null,它将设置为零,但不起作用.

My problem is that I can't find a way to default a rating to 0 if there are no defensive players found.
I've tried creating a list of the defensive players and if that list returns null, it will set it to zero but it doesn't work.

此代码在我的Teams班级中:(我也有相同类型的总体和进攻计算器,但前者使用所有玩家,后者使用进攻性玩家)
[code]
public int DefenceRatingCalc
{
GET
{VAR
从防御p =在玩家
其中p.position == PlayerPosition.DE ||
p.position == PlayerPosition.DT ||
p.position == PlayerPosition.OLB ||
p.position == PlayerPosition.MLB ||
p.position == PlayerPosition.CB ||
p.position == PlayerPosition.FS ||
p.position == PlayerPosition.SS
的OrderBy p.position
选择磷;
变种DR = defense.Average(( p)=> p.OVR);
ating = Convert.ToInt32(dR);
返回returnRingRating;
}
}
[/code]

This code is in my Teams class: (I also have Overall and Offense calculators of the same type but the former uses all players and the latter uses Offensive players)
[code]
        public int DefenseRatingCalc
        {
            get
            {
                var defense = from p in Players
                              where p.position == PlayerPosition.DE ||
                              p.position == PlayerPosition.DT ||
                              p.position == PlayerPosition.OLB ||
                              p.position == PlayerPosition.MLB ||
                              p.position == PlayerPosition.CB ||
                              p.position == PlayerPosition.FS ||
                              p.position == PlayerPosition.SS
                              orderby p.position
                              select p;
                var dR = defense.Average((p) => p.OVR);
                DefenseRating = Convert.ToInt32(dR);
                return DefenseRating;
            }
        }
[/code]

玩家属性位置"是PlayerPosition类型-一个包含以下内容的枚举:
[code]
public枚举PlayerPosition {QB,RB,WR,TE,LT,LG,C,RG,RT,DE,DT, OLB,MLB,CB,FS,SS}
[/code]
OVR也是Player类的属性.

The player property "position" is a PlayerPosition type--an enum that contains the following:
[code]
public enum PlayerPosition { QB, RB, WR, TE, LT, LG, C, RG, RT, DE, DT, OLB, MLB, CB, FS, SS }
[/code]
OVR is also a property of the Player class.

这是捕获所有防御性玩家的查询.
[code]
public List< Player> DefensivePlayers
{
GET
{VAR
名单=从p在玩家
其中p.position == PlayerPosition.DE ||
p.position = = PlayerPosition.DT ||
p.position == PlayerPosition.OLB ||
/> p.position == PlayerPosition.FS ||
p.position == PlayerPosition.SS
的OrderBy p.position
选择磷;
返回list.ToList();
}
}
[/code]

This is the query that grabs all of the defensive players.
[code]
       public List<Player> DefensivePlayers
        {
            get
            {
                var list = from p in Players
                           where p.position == PlayerPosition.DE ||
                           p.position == PlayerPosition.DT ||
                           p.position == PlayerPosition.OLB ||
                           p.position == PlayerPosition.MLB ||
                           p.position == PlayerPosition.CB ||
                           p.position == PlayerPosition.FS ||
                           p.position == PlayerPosition.SS
                           orderby p.position
                           select p;
                return list.ToList();
            }
        }
[/code]

这是我的调用方法:

[code]
foreach(联盟中的团队t.Teams)//in Main()
WriteLine(t.Name +" + t.昵称);
Console.WriteLine(<总体评级:+ t.OverallRatingCalc); < + t.OffenseRatingCalc);
Console.WriteLine(防御等级:" + t.DefenseRatingCalc); //计算评分并设置团队属性"DefenseRating"

[code]
            foreach (Team t in league.Teams)  //in Main()
            {
                Console.WriteLine("");
                Console.WriteLine(t.Name + " " + t.Nickname);
                Console.WriteLine("Overall Rating: " + t.OverallRatingCalc);
                Console.WriteLine("Offense Rating: " + t.OffenseRatingCalc);
                Console.WriteLine("Defense Rating: " + t.DefenseRatingCalc); //calculate rating and set team property "DefenseRating"
            }

[/code]

通过DefensivePlayers查询,我尝试了以下方法:

With the DefensivePlayers query, I tried this:

[code]
foreach(联盟中的团队t.Teams)
<");
" Console.WriteLine(t.Name +" + t.Nickname);" Console.WriteLine(tefenseD) //计算评分并设置团队属性``DefenseRating''
}被找到.");
}
}
[/code]

[code]
            foreach (Team t in league.Teams)
            {
                if (t.DefensivePlayers != null)
                {
                    Console.WriteLine("");
                    Console.WriteLine(t.Name + " " + t.Nickname);
                    Console.WriteLine("Defense Rating: " + t.DefenseRatingCalc); //calculate rating and set team property "DefenseRating"
                }
                else
                {
                    t.DefenseRating = 0;
                    Console.WriteLine("No defensive players were found.");
                }
            }
[/code]

但这是行不通的.我还在实际查询(DefenseRatingCalc)中尝试了这种方法,但这也不起作用.

but this doesn't work. I've also tried such a thing in the actual query (DefenseRatingCalc) but that doesn't work either.

推荐答案


这篇关于如果找不到玩家,则无法找到默认查询为0的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 16:48