TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Abhilash J A
528
2.4k
602.7k
list object if contain one value then return another one c#
Dec 23 2016 5:29 AM
Hello,
I have list items
The
Priority
list's object may contain 1 or 2 or 3.
If
Priority
contain 1 then return "Low"
If
Priority
contain 2 then return "Medium"
If
Priority
contain 3 then return "High"
I have tried...
public
class
DocumentsUser
{
public
string
Priority {
get
;
set
; }
}
List<DocumentsUser> objUserSetUp = objUserSetUp1.Select(m =>
new
DocumentsUser()
{
Priority = m.Priority == 3 ?
"High"
:
"Low"
,
//Here I want to manage that contition.
}
How can I do that?
Reply
Answers (
5
)
1
Ujjwal Gupta
0
1.1k
457.6k
Dec 23 2016 5:54 AM
public
class
DocumentsUser
{
public
string
Priority {
get
;
set
; }
}
List<DocumentsUser> objUserSetUp = objUserSetUp1.Select(m =>
new
DocumentsUser()
{
Priority = GetPriority(m.
Priority
);
} )
public
string
GetPriority(
int
priority)
{
switch
(priority)
{
case
1:
return
"Low"
;
break
;
case
2:
return
"Medium"
;
break
;
case
3:
return
"High"
;
break
;
Default:
return
"Low"
;
break
;
}
}
Accepted Answer
1
Nitin Sontakke
135
13.6k
14.9k
Dec 24 2016 1:47 AM
@Fabio,
Agreed in principle. However, just for record, specific to xml serialization, enum values can be decorated with an attribute as follows:
[
XmlEnum
(
Name
=
"High"
)]
1
Fabio Silva Lima
0
4.2k
1.2m
Dec 23 2016 9:37 AM
@Nitin I agree but it depends what he is trying to do. He can face a serialization problem with enum. If the frontend expects an integer when we use enum it's serialize as string and other problems with database.
@Ujjwal I do not like to use switch because a new priority you will need to add one more case. But i liked you separate method for the conversion.
1
Nitin Sontakke
135
13.6k
14.9k
Dec 23 2016 9:24 AM
Even better approach would be to have a property of enum's type as:
public PriorityType Priority {get; set;}
1
Fabio Silva Lima
0
4.2k
1.2m
Dec 23 2016 5:35 AM
create a enum and convert Priority.
public
class
DocumentsUser
{
public
string
Priority {
get
;
set
; }
}
public
enum
PriorityType{
High = 3,
Medium = 2,
Low = 1
}
var objUserSetUp = objUserSetUp1.Select(m => new {
Priority = (PriorityType)m.Priority});
data table to dictionary
Help Beginner please