I used a list to store the data from database and use it to take distinct from data.
public class Data
{
public int resultId;
public string resultTitle;
public string resultContent;
public string resultAuthor;
}
List<Data> dt = new List<Data>();
Data d = new Data();
d.resultTitle = reader["title"].ToString();
d.resultContent = reader["content"].ToString();
d.resultAuthor = reader["author"].ToString();
d.resultId = Convert.ToInt16(reader["content_id"]);
dt.Add(d);
but when i try to take the data out, im just getting the last result for the dt.count times.
for (int i = 0; i < dt.Count; i++)
{
response.write(dt[i].resultTitle );
}
please define where im having error and how to clear it. Is there any other way to store and retrieve the data like this.
As you are mentioning the List type as Data.. This Data needs to be a class which contains the variables such as resultTitle, resultContent, resultAuthor, resultID etc..
Then it is possible to store and retrieve the items from that list. (LINQ is a good option here)
Regards
Srikanth Kasturi
Please "Mark As Answer" if my post serves purpose.
cchidambaram
Member
136 Points
115 Posts
How to retrieve data from list below...
May 04, 2012 08:57 AM|LINK
Hi everyone,
I used a list to store the data from database and use it to take distinct from data.
public class Data { public int resultId; public string resultTitle; public string resultContent; public string resultAuthor; } List<Data> dt = new List<Data>(); Data d = new Data(); d.resultTitle = reader["title"].ToString(); d.resultContent = reader["content"].ToString(); d.resultAuthor = reader["author"].ToString(); d.resultId = Convert.ToInt16(reader["content_id"]); dt.Add(d);but when i try to take the data out, im just getting the last result for the dt.count times.
{please define where im having error and how to clear it. Is there any other way to store and retrieve the data like this.
data i need to store.
1,"a","a","a"
2,"b","b","b"
3,"c","c","c"
3,"c","c","c"
data i need to get distinct.
1,"a","a","a"
2,"b","b","b"
3,"c","c","c"
cchidambaram
Member
136 Points
115 Posts
Re: How to retrieve data from list below...
May 04, 2012 09:07 AM|LINK
Is it possible to use LINQ to get values from that list??
Srikanth Kas...
Contributor
4289 Points
883 Posts
Re: How to retrieve data from list below...
May 04, 2012 09:16 AM|LINK
As you are mentioning the List type as Data.. This Data needs to be a class which contains the variables such as resultTitle, resultContent, resultAuthor, resultID etc..
Then it is possible to store and retrieve the items from that list. (LINQ is a good option here)
Srikanth Kasturi
Please "Mark As Answer" if my post serves purpose.
cchidambaram
Member
136 Points
115 Posts
Re: How to retrieve data from list below...
May 04, 2012 09:32 AM|LINK
ya... i used a class with those variables. and my problem is i'm getting the last result for the number of times of count of list.
i tried for, foreach and linq but just getting same :(
Ramesh T
Contributor
5081 Points
822 Posts
Re: How to retrieve data from list below...
May 04, 2012 09:44 AM|LINK
Try this
List<Data> lst = new List<Data>(); lst.Add(new Data() { resultId = 1, resultAuthor = "a", resultContent = "a", resultTitle = "a" }); lst.Add(new Data() { resultId = 2, resultAuthor = "b", resultContent = "b", resultTitle = "b" }); lst.Add(new Data() { resultId = 3, resultAuthor = "b", resultContent = "b", resultTitle = "b" }); lst.Add(new Data() { resultId = 4, resultAuthor = "c", resultContent = "c", resultTitle = "c" }); var result = (from item in lst select item).Distinct(new Comparer()); //classes public class Comparer : IEqualityComparer<Data> { public bool Equals(Data x, Data y) { return x.resultTitle == y.resultTitle && x.resultContent == y.resultContent && x.resultAuthor == y.resultAuthor; } public int GetHashCode(Data obj) { return obj.resultAuthor.GetHashCode() ^ obj.resultContent.GetHashCode() ^ obj.resultTitle.GetHashCode(); } } public class Data { public int resultId; public string resultTitle; public string resultContent; public string resultAuthor; }Srikanth Kas...
Contributor
4289 Points
883 Posts
Re: How to retrieve data from list below...
May 04, 2012 09:46 AM|LINK
While retrieving, just use: var distinctNames = YourList.Distinct().ToList();
Srikanth Kasturi
Please "Mark As Answer" if my post serves purpose.
sravanisrav
Member
470 Points
107 Posts
Re: How to retrieve data from list below...
May 04, 2012 09:53 AM|LINK
Hi,
Make the variables as properties.Use get and set blocks
as:
public Class Data
{
public int pkid{ get;set;}
public string name{get;set;}
}
For more info on C# follow the link:
http://www.bestdotnettraining.com/Online/Training/CSharp/Language-Basics-img-src-OrimageOrfree-small-gif-border-alt-Or-/3
Thanks & Regards
Sravani
hrishirocks
Member
60 Points
17 Posts
Re: How to retrieve data from list below...
May 04, 2012 09:57 AM|LINK
Go for Array list instead.Something like this.
SqlDataReader dataReader = null;
ArrayList abc = new ArrayList();
while (dataReader.Read())
{
dataReader.GetValue(dataReader.GetOrdinal("column name")).ToString(),
abc.Add(temp);
}
Ruchira
All-Star
42900 Points
7021 Posts
MVP
Re: How to retrieve data from list below...
May 04, 2012 11:57 AM|LINK
Hello,
Try by putting this inside a while loop as below
while(reader.Read()) { d.resultTitle = reader["title"].ToString(); d.resultContent = reader["content"].ToString(); d.resultAuthor = reader["author"].ToString(); d.resultId = Convert.ToInt16(reader["content_id"]); dt.Add(d); }
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.