Reputation: 1855
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?
Upvotes: 4
Views: 9901
Reputation: 14672
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, i.e. false.
Upvotes: 0
Reputation: 4546
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!
Upvotes: 0
Reputation: 9869
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();
Upvotes: 1
Reputation: 1500495
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;
Upvotes: 12