Reputation: 95
I'm new to C# and am trying to use LinkedList<T>
class News {
private int Id { get; set; }
private string Name { get; set; }
public News (int id, string name) {
Id = id;
Name = name;
}
public string getName() {
return Name;
}
}
class Program {
static void Main(string[] args) {
var newsList = new LinkedList<LinkedListNode<News>>();
newsList.AddLast(new LinkedListNode<News>(new News(1,"News first")));
newsList.AddLast(new LinkedListNode<News>(new News(2, "News second")));
newsList.AddLast(new LinkedListNode<News>(new News(3, "News third")));
}
}
Once the above code runs, I wind up with newsList
containing 3 nodes, where node.Previous
and node.Next
are both null
. What am I doing wrong?
========================================= my actual code =========================
public ActionResult NewsDetail(int id = 0)
{
var an = db.News
.Select(n => new NewsView
{
Id = n.Id,
Name = n.Name,
Description = n.Description,
IsTop = n.IsTop,
IsDraft = n.IsDraft,
Content = n.Content,
CaruselContentType = n.CaruselContentType,
CaruselFileContent = n.CaruselFileContent,
CaruselFileName = n.CaruselFileName,
CaruselFileSize = n.CaruselFileSize,
NewsPartViews = n.NewsParts
.Select(
np => new NewsPartView
{
Id = np.Id,
SortNumber = np.SortNumber,
IsDraft = np.IsDraft,
Content = np.Content,
NewsId = np.NewsId,
NewsPartImagesPosition = np.NewsPartImagesPosition,
ImagesCols = np.ImagesCols,
ImagesWidth = np.ImagesWidth,
ImagesHeight = np.ImagesHeight,
ImagesBorder = np.ImagesBorder,
ImagesClickEnlarge = np.ImagesClickEnlarge,
NewsPartImageViews = np.NewsPartImages
.Select(
npi => new NewsPartImageView
{
Id = npi.Id
}
)
}
)
});
var newslList = new LinkedList<NewsView>(an);
return View(newslList.Find(newslList.First(view => view.Id == id)));
}
Upvotes: 0
Views: 2445
Reputation: 1456
If you are trying to add all the three items to the list, use this,
var newsItems = new News[] {
new News(1,"News first"),
new News(2, "News second"),
new News(3, "News third")};
var newsList = new LinkedList<News>(newsItems);
Upvotes: 1
Reputation: 203827
You shouldn't have a LinkedList<LinkedListNode<News>>
. You should be using a LinkedList<News>
. The LinkedList
will implicitly be wrapping each element in it's own LinkedListNode
. What's happening for you is that there's a bunch of LinkedListNode<LinkedListNode<News>>
objects, and the Next
/Previous
of the outer nodes are all set appropriately, but your inner nodes all have null
for the Next
/Previous
properties.
Here is an appropriate method of creating the LinkedList
.
var newsList = new LinkedList<News>();
newsList.AddLast(new News(1,"News first"));
newsList.AddLast(new News(2, "News second"));
newsList.AddLast(new News(3, "News third"));
Also note that LinkedList
is usually not the best data structure to use; it's rarely preferable to another type of collection. Odds are you're better off using either a List
, Stack
, a Queue
, depending on what you're doing with it.
Upvotes: 5
Reputation: 343
When using a linkedlist the type T does not need to be a LinkedListNode of the desired type. You can use the desired type directly.
var newsList = new LinkedList<News>();
newsList.AddLast(new LinkedListNode<News>(new News(1, "News first")));
newsList.AddLast(new LinkedListNode<News>(new News(2, "News second")));
newsList.AddLast(new LinkedListNode<News>(new News(3, "News third")));
This will setup your linked list the way you wanted.
Upvotes: 1
Reputation: 3970
It seems to me that your LinkedListNode objects have only one item each; according to the MSDN documentation,
LinkedListNode.Previous Property: A reference to the previous node in the LinkedList, or null if the current node is the first element (First) of the LinkedList.
LinkedListNode.Next Property: A reference to the next node in the LinkedList, or null if the current node is the last element (Last) of the LinkedList.
If there's only one element, it is both the first and the last - thus you'll get NULL results for both methods.
Upvotes: 0