如果找不到玩家,则无法找到默认查询为0的方法 [英] Can't find a way for query to default to 0 if no players are found

查看:53
本文介绍了如果找不到玩家,则无法找到默认查询为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.

推荐答案

该方法只是失败了,因为为了计算列表上的某个平均值,您应该将其除以列表中的元素.如果列表为空,则表示除以零.

解决方案非常简单,只需检查查询是否返回了至少一个元素即可. .
if(defense.Count()> 0){
... ...
} else {
返回0;
}

一些旧约注释:
1)为什么要指定orderby p.position子句?您似乎在任何地方都没有使用该排序.
2)我不知道查询的大小,但是我建议使用一种方法进行潜在的昂贵调用,而不是使用属性获取器.

HTH
--mc

The method simply fails because in order to compute some average on a list you are supposed to divide by the number of elements in the list. If the list is empty this implies dividing by zero.

The solution is fairly simple, just check that the query returned at least one element.

var defense = from ...
if (defense.Count () > 0) {
  ...
} else {
  return 0;
}

A few OT comments:
1) Why are you specifying the orderby p.position clause? You don't seem to be using the ordering anywhere.
2) I don't know the size of your query, but I would recommend to use a method for potentially expensive calls instead of a property getter.

HTH
--mc


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

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