Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a class with two property and i fill that with linq query

C#
public class a
{
    public int ID { set; get; }
    public string Name { set; get; }
}


passed
now i passed my generic list to the below function

C#
List<a> a1 = new List<a>();
a1 = (from p in context.TblTest select new a { ID = p.ID, Name = p.Name }).ToList();
TestFunction(a1);


my problem is how to use data in function?

C#
static public void WriteToFile<T>(List<T> YourList)
{
 //here 
// how to use ?
}



i want when i send List<anyclass> to this function the function writ data to excel file
like this
C#
for (int i = 0; i < YourList.count(); i++) excelWorksheet.Cells[i + 1, "A"] = //here what can i to do ;
Posted
Updated 27-Sep-13 14:00pm
v2
Comments
Zoltán Zörgő 27-Sep-13 17:13pm    
It depends on what you want to do.
aref.bozorgmehr 27-Sep-13 19:42pm    
i want when i send item<anyclass> to this function the function writ data to excel file

for (int i = 0; i < YourList.count(); i++)
excelWorksheet.Cells[i + 1, "A"] = //here what can i to do ;

You just need to read on generics; just one answer won't help you much: http://msdn.microsoft.com/en-us/library/512aeb7t.aspx[^].

In your case, look at this:
C#
static public void WriteToFile<T>(List<T> yourList)
{
    if (yourList == null) return;
    if (yourList.Count < 1) return;
    T first = yourList[0]; // look here
    //...
}

—SA
 
Share this answer
 
Comments
aref.bozorgmehr 27-Sep-13 19:07pm    
I know that, when i use your way and i get result from first.tostring() inseted data i have below result
mynamespace.a
Sergey Alexandrovich Kryukov 27-Sep-13 20:21pm    
What's the problem them? And did you read on generics?
—SA
aref.bozorgmehr 27-Sep-13 20:31pm    
yes i read generic but i want when i send List<class> to this function the function writ data to excel file
like this
for (int i = 0; i < YourList.count(); i++)
{
excelWorksheet.Cells[i + 1, "A"] = ID;
excelWorksheet.Cells[i + 1, "B"] = Name;
}
Sergey Alexandrovich Kryukov 27-Sep-13 21:32pm    
And how is this related to generics and your question?
—SA
aref.bozorgmehr 27-Sep-13 21:49pm    
i don't have problem with excel i want get data from function like my example but when i send data for function i don't know how can i receive that and use in a loop
I think you just want to iterate through the list items index based in for loop

XML
static public void WriteToFile<T>(List<T> YourList)
{
for (int i = 0; i < YourList.count(); i++) 
{
   excelWorksheet.Cells[i + 1, "A"] = yourList[i].ID;
   excelWorksheet.Cells[i + 1, "B"] = yourList[i].Name;
}
}


Or should be using foreach statement like

foreach(var item in yourList)
{
// you should increment i
excelWorksheet.Cells[i + 1, "A"] = item.ID;
}

Hope this helps
 
Share this answer
 
Comments
aref.bozorgmehr 28-Sep-13 19:04pm    
thanks but when we use " WriteToFile<t>(List<t> YourList)" can't to use Name and ID
your way is true when we use "static public void WriteToFile(List YourList)"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900