Link to home
Start Free TrialLog in
Avatar of pttoy
pttoy

asked on

C# syntax (class library returning a generic list)


This should be simple for C# developers.  I am new to the language and syntax.

Here is some basic VB.net code which I use all the time to access a class library call which returns a list(of structure)

How would I write this code in C#.

        Dim myEmp As New clsLibrary.Employees
        Dim myList As New List(Of clsLibrary.Employees.Employees_Structure)
        myList = myEmp.getStructEmployeeList("SEN", "Active")

        Dim query = From emp In myList Order By emp.LastName, emp.FirstName

        For Each item In query
            Response.Write(item.LastName & "<br/>")
        Next

Thanks!
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

clsLibrary.Employees myEmp = new clsLibrary.Employees();
List<clsLibrary.Employees.Employees_Structure> myList = new List<clsLibrary.Employees.Employees_Structure>();
myList = myEmp.getStructEmployeeList("SEN", "Active");

dynamic query = from emp in myListorderby emp.LastName, emp.FirstName;

foreach (object item_loopVariable in query) {
	item = item_loopVariable;
	Response.Write(item.LastName + "<br/>");
}

Open in new window

Avatar of pttoy
pttoy

ASKER

Close, but not quite there yet....

I am getting the following error message at the below line.

dynamic query = from emp in myListorderby emp.LastName, emp.FirstName

Error Message:
cannot implicitly convert type object to System.Collection.Generic.List<clsLibrary.Employees.Employees_Structure>
An explicit conversion exists (are you missing a cast?)

Thanks,
Please try the following this version is closer to your VB.NET code:

// .NET Framework 3.0+

var myEmp = new clsLibrary.Employees();
var myList = new List<clsLibrary.Employees.Employees_Structure>();
myList = myEmp.getStructEmployeeList("SEN", "Active");

var query = from emp in myList orderby emp.LastName, emp.FirstName;

foreach (var item in query)
{
	Response.Write(item.LastName + "<br/>");
}

Open in new window

Oops. I missed the select on the Linq query.  There you go:

// .NET Framework 3.0+

var myEmp = new clsLibrary.Employees();
var myList = new List<clsLibrary.Employees.Employees_Structure>();
myList = myEmp.getStructEmployeeList("SEN", "Active");

var query = from emp in myList orderby emp.LastName, emp.FirstName select emp;

foreach (var item in query)
{
	Response.Write(item.LastName + "<br/>");
}

Open in new window

Avatar of pttoy

ASKER


Thank You wdosanjos,

I'm still getting an error at the following line:
myList = myEmp.getStructEmployeeList("SEN", "Active");

Error Message:
cannot implicitly convert type object to System.Collection.Generic.List<clsLibrary.Employees.Employees_Structure>
An explicit conversion exists (are you missing a cast?)


ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pttoy

ASKER

Thank You