Iterate through object properties and update if condition is met
up vote
0
down vote
favorite
Currently I have a list, and I iterate through it, and If a condition is met I clear the data (see foreach please).
Current code but not really what I want, I want to use strongly typed
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
public ViewCommonItemVM()
{
Data = new List<string>();
}
}
List<ViewCommonItemVM> commons = new List<ViewCommonItemVM>();
commons.Add( new ViewCommonItemVM { Name = "Projects", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Companies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Schools", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Hobbies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Locations", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Interests", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Stuff", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Things", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Somelist", Data = someListHere });
foreach (var common in commons.Where(c => c.Data.Count != 0))
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
But I want to benefit from the stronly typed, so
Want to do
Object
public class ViewCommonVm
{
public ViewCommonItemVM Projects {get;set;}
public ViewCommonItemVM Companies {get;set;}
public ViewCommonItemVM Schools {get;set;}
public ViewCommonItemVM Hobbies {get;set;}
public ViewCommonItemVM Locations {get;set;}
public ViewCommonItemVM Interests {get;set;}
public ViewCommonItemVM Stuff {get;set;}
public ViewCommonItemVM Things {get;set;}
public ViewCommonItemVM Somelist {get;set;}
}
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
}
// Here it should iterated trough the object, and after
// 4 lenght properties != 0, clear the property list
How can I achieve this?
c# .net
add a comment |
up vote
0
down vote
favorite
Currently I have a list, and I iterate through it, and If a condition is met I clear the data (see foreach please).
Current code but not really what I want, I want to use strongly typed
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
public ViewCommonItemVM()
{
Data = new List<string>();
}
}
List<ViewCommonItemVM> commons = new List<ViewCommonItemVM>();
commons.Add( new ViewCommonItemVM { Name = "Projects", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Companies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Schools", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Hobbies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Locations", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Interests", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Stuff", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Things", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Somelist", Data = someListHere });
foreach (var common in commons.Where(c => c.Data.Count != 0))
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
But I want to benefit from the stronly typed, so
Want to do
Object
public class ViewCommonVm
{
public ViewCommonItemVM Projects {get;set;}
public ViewCommonItemVM Companies {get;set;}
public ViewCommonItemVM Schools {get;set;}
public ViewCommonItemVM Hobbies {get;set;}
public ViewCommonItemVM Locations {get;set;}
public ViewCommonItemVM Interests {get;set;}
public ViewCommonItemVM Stuff {get;set;}
public ViewCommonItemVM Things {get;set;}
public ViewCommonItemVM Somelist {get;set;}
}
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
}
// Here it should iterated trough the object, and after
// 4 lenght properties != 0, clear the property list
How can I achieve this?
c# .net
Do you want to remove property from object or do you want to remove all items from list if condition is met? If it's second one, you can't remove items from list when iterating it, so what you can do is something like this: var count=0; foreach (var common in commons.Where(c => c.Data.Count != 0)) { count++; } if (count > 4 && common.Data.Count != 0) commons.Clear();
– Kadaj
Jan 16 '17 at 10:34
@Kadaj, I don't want to use list
– Gerald Hughes
Jan 16 '17 at 10:35
1
[Type.GetProperties][1] will list each of the properties of a given type. Then use [PropertyInfo.GetValue][2] to check the values. [1]: msdn.microsoft.com/en-us/library/system.type.getproperties.aspx [2]: msdn.microsoft.com/en-us/library/b05d59ty.aspx
– Kadaj
Jan 16 '17 at 10:42
1
If you are going to do this(Reflection) only for this class then it is of no use and it'll add performance overhead. If you planned to use generics or interfaces or abstarcts and the type you doesn't know in runtime then it'll be great to use. Unless otherwise as I said do not use reflection for an object, for which you know the type at runtime.
– Karthik AMR
Jan 16 '17 at 11:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Currently I have a list, and I iterate through it, and If a condition is met I clear the data (see foreach please).
Current code but not really what I want, I want to use strongly typed
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
public ViewCommonItemVM()
{
Data = new List<string>();
}
}
List<ViewCommonItemVM> commons = new List<ViewCommonItemVM>();
commons.Add( new ViewCommonItemVM { Name = "Projects", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Companies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Schools", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Hobbies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Locations", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Interests", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Stuff", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Things", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Somelist", Data = someListHere });
foreach (var common in commons.Where(c => c.Data.Count != 0))
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
But I want to benefit from the stronly typed, so
Want to do
Object
public class ViewCommonVm
{
public ViewCommonItemVM Projects {get;set;}
public ViewCommonItemVM Companies {get;set;}
public ViewCommonItemVM Schools {get;set;}
public ViewCommonItemVM Hobbies {get;set;}
public ViewCommonItemVM Locations {get;set;}
public ViewCommonItemVM Interests {get;set;}
public ViewCommonItemVM Stuff {get;set;}
public ViewCommonItemVM Things {get;set;}
public ViewCommonItemVM Somelist {get;set;}
}
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
}
// Here it should iterated trough the object, and after
// 4 lenght properties != 0, clear the property list
How can I achieve this?
c# .net
Currently I have a list, and I iterate through it, and If a condition is met I clear the data (see foreach please).
Current code but not really what I want, I want to use strongly typed
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
public ViewCommonItemVM()
{
Data = new List<string>();
}
}
List<ViewCommonItemVM> commons = new List<ViewCommonItemVM>();
commons.Add( new ViewCommonItemVM { Name = "Projects", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Companies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Schools", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Hobbies", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Locations", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Interests", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Stuff", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Things", Data = someListHere });
commons.Add( new ViewCommonItemVM { Name = "Somelist", Data = someListHere });
foreach (var common in commons.Where(c => c.Data.Count != 0))
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
But I want to benefit from the stronly typed, so
Want to do
Object
public class ViewCommonVm
{
public ViewCommonItemVM Projects {get;set;}
public ViewCommonItemVM Companies {get;set;}
public ViewCommonItemVM Schools {get;set;}
public ViewCommonItemVM Hobbies {get;set;}
public ViewCommonItemVM Locations {get;set;}
public ViewCommonItemVM Interests {get;set;}
public ViewCommonItemVM Stuff {get;set;}
public ViewCommonItemVM Things {get;set;}
public ViewCommonItemVM Somelist {get;set;}
}
public class ViewCommonItemVM
{
public string Name { get; set; }
public List<string> Data { get; set; }
public string Bullet { get; set; }
}
// Here it should iterated trough the object, and after
// 4 lenght properties != 0, clear the property list
How can I achieve this?
c# .net
c# .net
edited Nov 12 at 5:52
Cœur
17.3k9102142
17.3k9102142
asked Jan 16 '17 at 10:16
Gerald Hughes
1,81753476
1,81753476
Do you want to remove property from object or do you want to remove all items from list if condition is met? If it's second one, you can't remove items from list when iterating it, so what you can do is something like this: var count=0; foreach (var common in commons.Where(c => c.Data.Count != 0)) { count++; } if (count > 4 && common.Data.Count != 0) commons.Clear();
– Kadaj
Jan 16 '17 at 10:34
@Kadaj, I don't want to use list
– Gerald Hughes
Jan 16 '17 at 10:35
1
[Type.GetProperties][1] will list each of the properties of a given type. Then use [PropertyInfo.GetValue][2] to check the values. [1]: msdn.microsoft.com/en-us/library/system.type.getproperties.aspx [2]: msdn.microsoft.com/en-us/library/b05d59ty.aspx
– Kadaj
Jan 16 '17 at 10:42
1
If you are going to do this(Reflection) only for this class then it is of no use and it'll add performance overhead. If you planned to use generics or interfaces or abstarcts and the type you doesn't know in runtime then it'll be great to use. Unless otherwise as I said do not use reflection for an object, for which you know the type at runtime.
– Karthik AMR
Jan 16 '17 at 11:08
add a comment |
Do you want to remove property from object or do you want to remove all items from list if condition is met? If it's second one, you can't remove items from list when iterating it, so what you can do is something like this: var count=0; foreach (var common in commons.Where(c => c.Data.Count != 0)) { count++; } if (count > 4 && common.Data.Count != 0) commons.Clear();
– Kadaj
Jan 16 '17 at 10:34
@Kadaj, I don't want to use list
– Gerald Hughes
Jan 16 '17 at 10:35
1
[Type.GetProperties][1] will list each of the properties of a given type. Then use [PropertyInfo.GetValue][2] to check the values. [1]: msdn.microsoft.com/en-us/library/system.type.getproperties.aspx [2]: msdn.microsoft.com/en-us/library/b05d59ty.aspx
– Kadaj
Jan 16 '17 at 10:42
1
If you are going to do this(Reflection) only for this class then it is of no use and it'll add performance overhead. If you planned to use generics or interfaces or abstarcts and the type you doesn't know in runtime then it'll be great to use. Unless otherwise as I said do not use reflection for an object, for which you know the type at runtime.
– Karthik AMR
Jan 16 '17 at 11:08
Do you want to remove property from object or do you want to remove all items from list if condition is met? If it's second one, you can't remove items from list when iterating it, so what you can do is something like this: var count=0; foreach (var common in commons.Where(c => c.Data.Count != 0)) { count++; } if (count > 4 && common.Data.Count != 0) commons.Clear();
– Kadaj
Jan 16 '17 at 10:34
Do you want to remove property from object or do you want to remove all items from list if condition is met? If it's second one, you can't remove items from list when iterating it, so what you can do is something like this: var count=0; foreach (var common in commons.Where(c => c.Data.Count != 0)) { count++; } if (count > 4 && common.Data.Count != 0) commons.Clear();
– Kadaj
Jan 16 '17 at 10:34
@Kadaj, I don't want to use list
– Gerald Hughes
Jan 16 '17 at 10:35
@Kadaj, I don't want to use list
– Gerald Hughes
Jan 16 '17 at 10:35
1
1
[Type.GetProperties][1] will list each of the properties of a given type. Then use [PropertyInfo.GetValue][2] to check the values. [1]: msdn.microsoft.com/en-us/library/system.type.getproperties.aspx [2]: msdn.microsoft.com/en-us/library/b05d59ty.aspx
– Kadaj
Jan 16 '17 at 10:42
[Type.GetProperties][1] will list each of the properties of a given type. Then use [PropertyInfo.GetValue][2] to check the values. [1]: msdn.microsoft.com/en-us/library/system.type.getproperties.aspx [2]: msdn.microsoft.com/en-us/library/b05d59ty.aspx
– Kadaj
Jan 16 '17 at 10:42
1
1
If you are going to do this(Reflection) only for this class then it is of no use and it'll add performance overhead. If you planned to use generics or interfaces or abstarcts and the type you doesn't know in runtime then it'll be great to use. Unless otherwise as I said do not use reflection for an object, for which you know the type at runtime.
– Karthik AMR
Jan 16 '17 at 11:08
If you are going to do this(Reflection) only for this class then it is of no use and it'll add performance overhead. If you planned to use generics or interfaces or abstarcts and the type you doesn't know in runtime then it'll be great to use. Unless otherwise as I said do not use reflection for an object, for which you know the type at runtime.
– Karthik AMR
Jan 16 '17 at 11:08
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Are you able to use linq to solve this problem?
Something like this may work
commons.Where(x => x.Data.Any()).Skip(4).ToList().ForEach(x => x.Data = new List<string>());
add a comment |
up vote
0
down vote
Must use reflection:
void MyClear()
{
int count = 0;
foreach (System.Reflection.PropertyInfo item in typeof(ViewCommonVm).GetProperties())
{
ViewCommonItemVM common = (ViewCommonItemVM)item.GetValue(this);
if (common.Data.Count() != 0)
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
}
}
A bit slower than the original. If you have a property which is not ViewCommonItemVM, change the code to check whether item.GetValue(this) results proper type.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41674029%2fiterate-through-object-properties-and-update-if-condition-is-met%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Are you able to use linq to solve this problem?
Something like this may work
commons.Where(x => x.Data.Any()).Skip(4).ToList().ForEach(x => x.Data = new List<string>());
add a comment |
up vote
0
down vote
Are you able to use linq to solve this problem?
Something like this may work
commons.Where(x => x.Data.Any()).Skip(4).ToList().ForEach(x => x.Data = new List<string>());
add a comment |
up vote
0
down vote
up vote
0
down vote
Are you able to use linq to solve this problem?
Something like this may work
commons.Where(x => x.Data.Any()).Skip(4).ToList().ForEach(x => x.Data = new List<string>());
Are you able to use linq to solve this problem?
Something like this may work
commons.Where(x => x.Data.Any()).Skip(4).ToList().ForEach(x => x.Data = new List<string>());
answered Jan 16 '17 at 10:43
Bad Dub
4951426
4951426
add a comment |
add a comment |
up vote
0
down vote
Must use reflection:
void MyClear()
{
int count = 0;
foreach (System.Reflection.PropertyInfo item in typeof(ViewCommonVm).GetProperties())
{
ViewCommonItemVM common = (ViewCommonItemVM)item.GetValue(this);
if (common.Data.Count() != 0)
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
}
}
A bit slower than the original. If you have a property which is not ViewCommonItemVM, change the code to check whether item.GetValue(this) results proper type.
add a comment |
up vote
0
down vote
Must use reflection:
void MyClear()
{
int count = 0;
foreach (System.Reflection.PropertyInfo item in typeof(ViewCommonVm).GetProperties())
{
ViewCommonItemVM common = (ViewCommonItemVM)item.GetValue(this);
if (common.Data.Count() != 0)
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
}
}
A bit slower than the original. If you have a property which is not ViewCommonItemVM, change the code to check whether item.GetValue(this) results proper type.
add a comment |
up vote
0
down vote
up vote
0
down vote
Must use reflection:
void MyClear()
{
int count = 0;
foreach (System.Reflection.PropertyInfo item in typeof(ViewCommonVm).GetProperties())
{
ViewCommonItemVM common = (ViewCommonItemVM)item.GetValue(this);
if (common.Data.Count() != 0)
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
}
}
A bit slower than the original. If you have a property which is not ViewCommonItemVM, change the code to check whether item.GetValue(this) results proper type.
Must use reflection:
void MyClear()
{
int count = 0;
foreach (System.Reflection.PropertyInfo item in typeof(ViewCommonVm).GetProperties())
{
ViewCommonItemVM common = (ViewCommonItemVM)item.GetValue(this);
if (common.Data.Count() != 0)
{
count++;
if (count > 4 && common.Data.Count != 0)
common.Data.Clear();
}
}
}
A bit slower than the original. If you have a property which is not ViewCommonItemVM, change the code to check whether item.GetValue(this) results proper type.
answered Jan 16 '17 at 10:59
Peter Krassoi
32319
32319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41674029%2fiterate-through-object-properties-and-update-if-condition-is-met%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you want to remove property from object or do you want to remove all items from list if condition is met? If it's second one, you can't remove items from list when iterating it, so what you can do is something like this: var count=0; foreach (var common in commons.Where(c => c.Data.Count != 0)) { count++; } if (count > 4 && common.Data.Count != 0) commons.Clear();
– Kadaj
Jan 16 '17 at 10:34
@Kadaj, I don't want to use list
– Gerald Hughes
Jan 16 '17 at 10:35
1
[Type.GetProperties][1] will list each of the properties of a given type. Then use [PropertyInfo.GetValue][2] to check the values. [1]: msdn.microsoft.com/en-us/library/system.type.getproperties.aspx [2]: msdn.microsoft.com/en-us/library/b05d59ty.aspx
– Kadaj
Jan 16 '17 at 10:42
1
If you are going to do this(Reflection) only for this class then it is of no use and it'll add performance overhead. If you planned to use generics or interfaces or abstarcts and the type you doesn't know in runtime then it'll be great to use. Unless otherwise as I said do not use reflection for an object, for which you know the type at runtime.
– Karthik AMR
Jan 16 '17 at 11:08