简体   繁体   中英

EF6 How to obtain data for DevExpress LookupEdit

I have a table structured as follows:

CREATE TABLE employee
(
    employeeid integer NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    familyname varchar(100) NOT NULL,
    givenname varchar(100) NOT NULL,
    position varchar(100) NOT NULL,
    startdate date,
    address varchar(250),
    address2 varchar(250),
    city varchar(100),
    provincecd character(2),
    countrycd character(2),
    postalcode character varying(15),
    enddate date,
    employmentstatusid integer,
    photo bytea,
    dob date,
);

This table and several others are configured into a C# EF6 project.

I have a VB.Net WinForms project which uses the EF6 Entities as it's data store.

I am using a DevExpress XtraGrid control which I use to interactively populate my database. The problem comes in where I need to provide data to a DevExpress LookupEdit control where I am using a subset of the data in my employees entity to populate the LookupEdit. The data I want to use can be represented by the following SQL query:

    SELECT
    employeeid
    , concat(familyname, ', ', givenname) as fullname
FROM
    employee
WHERE
    employmentstatusid in (1,2)
ORDER BY
    familyname
    , givenname

I am not sure how to obtain the data using EF6 to populate my LookupEdit.

I am a neophyte when it comes to EF, but have worked with ADO.Net DataSets for many years.

You use LINQ to Entities to query an EF DbContext . In your case, it's going to be something like this:

Dim employeeNamesById = myDbContext.employees.
                                    Where(Function(e) {1, 2}.Contains(e.employeestatusid)).
                                    OrderBy(Function(e) e.familyname).
                                    ThenBy(Function(e) e.givenname).
                                    Select(Function(e) New With
                                                       {
                                                           e.employeeid,
                                                           .fullname = e.familyname & ", " & e.givenname
                                                       }).
                                    ToList()

That will create a generic List of objects of an anonymous type. You may prefer to define a class with the appropriate properties and create instances of that instead. Also, that code uses function syntax, which is generally my preference. You can use query syntax if you prefer, but I'll leave that to you if you want it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM