Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bool value as returned object from the generics

Tags:

c#

linq

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?

like image 448
Searcher Avatar asked Dec 11 '22 23:12

Searcher


2 Answers

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;
like image 100
Jon Skeet Avatar answered Mar 31 '23 23:03

Jon Skeet


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();
like image 20
Yograj Gupta Avatar answered Mar 31 '23 23:03

Yograj Gupta