简体   繁体   中英

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. For that one I written like that. But it is giving error as

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'

at select p.export How to resolve this?

The problem is that your query is retrieve a sequence of bool values - one for each record matching your filter. Presumably you're only expecting a single result, so you could use 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. 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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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