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!
.NET Programming

Avatar of undefined
Last Comment
pttoy

8/22/2022 - Mon
Paul Jackson

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

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,
wdosanjos

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
wdosanjos

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

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
wdosanjos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pttoy

ASKER
Thank You
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.