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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With