Property

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following properties in a class:

public List<ObjA> A { get; set; }
public string B { get; set; }

B is always an array created from A.Name property in all A property.

How can I define B as described? I am using Net 3.5 so I can use Linq.

Thanks,
Miguel
 
I have the following properties in a class:

    public List<ObjA> A { get; set; }
    public string B { get; set; }

B is always an array created from A.Name property in all A property.

B isn't an array at all, it's a string. Did you mean to declare it as
a string[]?
How can I define B as described? I am using Net 3.5 so I can use Linq.

Should B really be writable? If not, you might want:

public string[] B
{
get { return A.Select(x => x.Name).ToArray(); }
}

Jon
 
I have the following properties in a class:
    public List<ObjA> A { get; set; }
    public string B { get; set; }
B is always an array created from A.Name property in all A property.

B isn't an array at all, it's a string. Did you mean to declare it as
a string[]?
How can I define B as described? I am using Net 3.5 so I can use Linq.

Should B really be writable? If not, you might want:

public string[] B
{
    get { return A.Select(x => x.Name).ToArray(); }

}

Jon

I did a mistake: I want it to be in a CSV format so I changed your
code to:
string.Join(", ", return A.Select(x => x.Name).ToArray());

Is this ok? Maybe I could do this directly from the list?

And yes it should be writable.

Thanks,
Miguel
 
I did a mistake: I want it to be in a CSV format so I changed your
code to:
string.Join(", ", return A.Select(x => x.Name).ToArray());

Is this ok? Maybe I could do this directly from the list?

Your "return" is in the wrong place, but otherwise it should work.
It's not terribly efficient (because ToArray will copy everything),
but it'll work.
And yes it should be writable.

What will you make it do if someone writes to it then? Split the
value, check the length is right and assign the names within A?
Doesn't sound terribly nice.

Jon
 
Your "return" is in the wrong place, but otherwise it should work.
It's not terribly efficient (because ToArray will copy everything),
but it'll work.


What will you make it do if someone writes to it then? Split the
value, check the length is right and assign the names within A?
Doesn't sound terribly nice.

Jon

Yes, problem when typing. It is:
string.Join(", ", A.Select(x => x.Name).ToArray());

So how should I do it to make it more efficient?

The writable part is still under thinking ... this class is ViewData
to communicate between a View and a Controller.
 
Yes, problem when typing. It is:
  string.Join(", ", A.Select(x => x.Name).ToArray());

So how should I do it to make it more efficient?

Unfortunately I suspect you'd need to write your own implementation of
String.Join. (It takes an array, right?)
The writable part is still under thinking ... this class is ViewData
to communicate between a View and a Controller.

But does that part need to be modifiable?

Jon
 
Unfortunately I suspect you'd need to write your own implementation of
String.Join. (It takes an array, right?)


But does that part need to be modifiable?

Jon

I am a little bit confuse about this. I am using the following:

public class PostPaper {

public Post Post { get; set; }
public List<Tag> Tags {
get;
set {
Tags = this.TagsCSV.Split(',').Select(t => new Tag { Name =
t.Trim() }).ToList();
}
}
public string TagsCSV {
get {
return string.Join(", ", this.Tags.Select(t =>
t.Name).ToArray()); ;
}
set;
}

Sometimes I define Tags by converting from TagsCSV ... but sometime I
can also do this using Linq as follows:

PostPaper paper = (from p in database.Posts
where p.PostID == id
select new PostPaper {
Post =
p,
Tags = (from pt in database.PostsTags
join t in database.Tags on pt.TagID
equals t.TagID
where pt.PostID == id
orderby t.Name
select t).ToList()
}).SingleOrDefault();
}

Can I integrate this on my properties? Or maybe on my class?

Thanks,
Miguel
 
shapper said:
I am a little bit confuse about this. I am using the following:

public class PostPaper {

public Post Post { get; set; }
public List<Tag> Tags {
get;
set {
Tags = this.TagsCSV.Split(',').Select(t => new Tag { Name =
t.Trim() }).ToList();
}
}

You've got a problem there to start with - you're assigning to Tags
from within Tags, and you haven't provided a getter.

Sometimes I define Tags by converting from TagsCSV ... but sometime I
can also do this using Linq as follows:

PostPaper paper = (from p in database.Posts
where p.PostID == id
select new PostPaper {
Post =
p,
Tags = (from pt in database.PostsTags
join t in database.Tags on pt.TagID
equals t.TagID
where pt.PostID == id
orderby t.Name
select t).ToList()
}).SingleOrDefault();
}

Can I integrate this on my properties? Or maybe on my class?

I don't think we know nearly enough about what you're trying to do at
this point, I'm afraid. I doubt that you really want to do a database
lookup each time someone accesses a property though.
 

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

Similar Threads

Intersection of Lists 5
Override property 18
Linq. Aggregate 4
Property Default Value 3
Property Default Values in C# 5
Get Strings 3
NHibernate QueryOver with Many-to-Many 0
FindAll. Linq. 4

Back
Top