LINQ with switch statement

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
};
 
F

Frans Bouma [C# MVP]

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#)
------------------------------------------------------------------------
 
A

Ant

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#)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top