简体   繁体   English

EF6 如何获取 DevExpress Lookup 的数据编辑

[英]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.此表和其他几个表被配置到 C# EF6 项目中。

I have a VB.Net WinForms project which uses the EF6 Entities as it's data store.我有一个 VB.Net WinForms 项目,它使用 EF6 实体作为数据存储。

I am using a DevExpress XtraGrid control which I use to interactively populate my database.我正在使用一个 DevExpress XtraGrid 控件,我用它来交互式地填充我的数据库。 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.问题出现在我需要向 DevExpress LookupEdit 控件提供数据的地方,我在其中使用员工实体中的数据子集来填充 LookupEdit。 The data I want to use can be represented by the following SQL query:我要使用的数据可以由以下 SQL 查询表示:

    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.我不确定如何使用 EF6 获取数据来填充我的 LookupEdit。

I am a neophyte when it comes to EF, but have worked with ADO.Net DataSets for many years.我是 EF 的新手,但多年来一直使用 ADO.Net DataSets。

You use LINQ to Entities to query an EF DbContext .您使用 LINQ to Entities 查询 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.这将创建一个匿名类型的对象的通用List 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.如果您愿意,可以使用查询语法,但如果您愿意,我会将其留给您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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