LINQ with switch statement

  • Thread starter Thread starter Ant
  • Start date Start date
A

Ant

Hi,
I'm trying to use a swithc statement in a LINQ query but its not ahppy at all.
Any ideas as to the correct way to go about this?

Many thanks for any ideas
Ant

Below is the statement:

var resultSet = from p in db.Projects
where p.ProjectCode == textboxProjectCode.Text &&
p.RevisionStatusCode == 'A'
select new
{
p.ProjectCode,
p.RevisionNum,
"StatusCode" =
switch (p.StatusCode)
{
case 'I':
"Inactivated";
break;
case 'E':
"Engaged";
break;
case 'C':
"Closed";
break;
case 'P':
"Planned";
break;
default:
"default";
break;
},
p.RevisionName,
p.LastUpdateUserID,
p.LastUpdateDate
};
 
Ant said:
Hi,
I'm trying to use a swithc statement in a LINQ query but its not
ahppy at all. Any ideas as to the correct way to go about this?

Many thanks for any ideas
Ant

Below is the statement:

var resultSet = from p in db.Projects
where p.ProjectCode ==
textboxProjectCode.Text &&
p.RevisionStatusCode == 'A' select new
{
p.ProjectCode,
p.RevisionNum,
"StatusCode" =
switch (p.StatusCode)
{
case 'I':
"Inactivated";
break;
case 'E':
"Engaged";
break;
case 'C':
"Closed";
break;
case 'P':
"Planned";
break;
default:
"default";
break;
},
p.RevisionName,
p.LastUpdateUserID,
p.LastUpdateDate
};

You've to add an in-memory method call. So move your switch to a method
'StatusCodeToString' and then in your query's projection:
....
select new {
p.ProjectCode,
p.RevisionNum,
StatusCode = StatusCodeToString(p.StatusCode),
p.RevisionName,
etc.
};

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Thank you very much Frans

Ant

Frans Bouma said:
You've to add an in-memory method call. So move your switch to a method
'StatusCodeToString' and then in your query's projection:
....
select new {
p.ProjectCode,
p.RevisionNum,
StatusCode = StatusCodeToString(p.StatusCode),
p.RevisionName,
etc.
};

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
 
Back
Top