简体   繁体   English

如何从泛型中获取bool值作为返回的对象

[英]How to get bool value as returned object from the generics

I have structure like this. 我有这样的结构。

 public class ExportStructure
    {
        public string issueid { get; set; }
        public string filename {get;set;}
        public bool export { get; set; }
    }
public class ExportStructureManager
    {
        public List<ExportStructure> ExportIntoStructure { get; set; }
        public ExportStructureManager()
        {
            this.ExportIntoStructure = new List<ExportStructure>();
        }
        public ExportStructure AddToStructure(string issueid,string filename,bool exportornot)
        {
            ExportStructure expstr = new ExportStructure();
            expstr.issueid = issueid;
            expstr.filename = filename;
            expstr.export = exportornot;

            this.ExportIntoStructure.Add(expstr);
            return (expstr);
        }
        public bool GetStatusFromStructure(string issuekey)
        {
          return (from p in ExportIntoStructure
                where p.issueid == issuekey
                select p.export));
        }
    }

From the above one, I want to execute GetStatusFromStructure such that it should return me the export property status. 从上面的一个,我想执行GetStatusFromStructure ,它应该返回我的export属性状态。 For that one I written like that. 对于那个我写的那样。 But it is giving error as 但是它给出了错误

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool' 无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'bool'

at select p.export How to resolve this? select p.export如何解决这个问题?

The problem is that your query is retrieve a sequence of bool values - one for each record matching your filter. 问题是您的查询检索了一系列 bool值 - 每个匹配您的过滤器的记录一个。 Presumably you're only expecting a single result, so you could use Single : 大概你只期望一个结果,所以你可以使用Single

return (from p in ExportIntoStructure
        // I assume you meant == rather than =
        where p.issueid == issuekey
        select p.export).Single();

But you should also consider what you want to happen if there are multiple results or none at all. 但是如果有多个结果或根本没有结果,你也应该考虑你想要发生什么。 The options are: 选项是:

  • Single
  • SingleOrDefault
  • First
  • FirstOrDefault
  • Last
  • LastOrDefault

Which one is appropriate depends on what you want the behaviour to be in those situations. 哪一个是合适的取决于您希望行为在这些情况下的行为。

You might also want to consider making this a non-query-expression: 您可能还想考虑将其设为非查询表达式:

return ExportIntoStructure.Where(p => p.issueid == issuekey)
                          .Select(p => p.export)
                          .Single();

Or even match to a single object first, then project: 或者甚至首先匹配单个对象,然后项目:

return ExportIntoStructure.Single(p => p.issueid == issuekey)
                          .export;

Change statement 变更声明

return (from p in ExportIntoStructure
            where p.issueid = issuekey
            select p.export));

to

return (from p in ExportIntoStructure
            where p.issueid == issuekey
            select p.export)).Single();

but be sure that issuekey is exists otherwise it will throw exception. 但请确保isskey存在,否则会抛出异常。 or try its Default. 或尝试其默认值。

return (from p in ExportIntoStructure
            where p.issueid == issuekey
            select p.export)).SingleOrDefault();

Add FirstOrDefault annonymous method on the end of thw query. 在查询结束时添加FirstOrDefault匿名方法。

return (from p in ExportIntoStructure
            where p.issueid = issuekey
            select p.export)).FirstOrDefault();

FirstOrDefault (or SinlgeOrDefault()) annonymous method is good if there is no result, it will return null, if you will use onoy First or Single, if no value, there is will an error thrown! FirstOrDefault(或SinlgeOrDefault())匿名方法是好的,如果没有结果,它将返回null,如果你将使用onoy First或Single,如果没有值,则会抛出一个错误!

Change the function to this: 将功能更改为:

    public bool GetStatusFromStructure(string issuekey) 
    { 
        return (from p in ExportIntoStructure 
                where p.issueid = issuekey 
                select p.export).FirstOrDefault()); 
    } 

But be aware that if there is no match it will be the boolean default, ie false. 但要注意,如果没有匹配,它将是布尔默认值,即false。

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

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