Entity Framework Linq 两张表一起格式化

Entity Framework Linq two tables together with formatting

我完全不了解 Entity Framework 以及它是如何工作的。在大多数情况下,我可以使用以下方法来满足 returns 一个列表,我可以将它绑定到类似 DropDownList 的东西:

    public static List<Data.Land> GetLandList()
    {
        List<Data.Land> ListEntities = null;

        using (Data.languagesEntities context = GetDbContext())
        {
            ListEntities = (from ent1 in context.Land
                            orderby ent1.Land_sortering ascending, ent1.Land1 ascending
                            select ent1).ToList<Data.Land>();
        }

        return ListEntities;
    }

但是对于另一个 DropDownList,我需要 "Linq" 将两个表放在一起并连接多个表格单元格中的值以获得我想要显示的所需文本。我最终得到了这段我无法提取到方法中的可怕代码:

public class DropdownDisplay
{
public string value { get; set; }
public string Text { get; set; }
}

void Input_DropDownList_SelectCourse_SelectedItemChanged(object sender, BootstrapUI.UserControls.BootstrapFormControls.DropDownListItemEventArgs e)
{
    int Cursus_ID = Convert.ToInt16(this.Input_DropDownList_SelectCourse.Value);

    Data.languagesEntities context = Data._languagesEntities.GetDbContext();

    var ListEntities = (from ent1 in context.Cursusdata
                        from ent2 in context.Cursus
                        where ent1.Cursus_ID == Cursus_ID
                        && ent1.Cursus_ID == ent2.Cursus_ID
                        && ent1.Cursusdata_gesloten == 0
                        orderby ent1.Cursusdata_startdatum ascending
                        select new
                        {
                            Cursusdata_ID = ent1.Cursusdata_ID,
                            Cursus_Code = ent2.Cursus_Code,
                            Cursusdata_einddatum = ent1.Cursusdata_einddatum,
                            Cursusdata_startdatum = ent1.Cursusdata_startdatum,
                            Cursusdata_tijden_NL = ent1.Cursusdata_tijden_NL,
                            Cursusdata_tijden_UK = ent1.Cursusdata_tijden_UK,
                            Cursus_ID = ent1.Cursus_ID
                        }
                        ).ToList();

    List<DropdownDisplay> List_DropdownDisplay = new List<DropdownDisplay>();
    foreach (var Entity in ListEntities)
    {
        string newStartDatum = (Entity.Cursusdata_startdatum == null) ? "" : ((DateTime)Entity.Cursusdata_startdatum).ToShortDateString();
        string newEindDatum = (Entity.Cursusdata_einddatum == null) ? "" : ((DateTime)Entity.Cursusdata_einddatum).ToShortDateString();

        List_DropdownDisplay.Add(new DropdownDisplay()
        {
            value = Entity.Cursusdata_ID.ToString(),
            Text =
                Entity.Cursus_Code + " - " + newStartDatum +
                " - " + newEindDatum + " - " + Entity.Cursusdata_tijden_NL
        });
    }

    this.Input_DropDownList_SelectPeriod.DataSource = List_DropdownDisplay;
    this.Input_DropDownList_SelectPeriod.DataValueField = "Value";
    this.Input_DropDownList_SelectPeriod.DataTextField = "Text";
    this.Input_DropDownList_SelectPeriod.DataBind();
  }

我应该如何以正确的方式完成此操作?

编辑:我应该澄清我的问题:t3chb0t 的回答确实以一种方法提取了我的解决方案,但我也想知道如何改进数据收集和操作。

非常感谢, 雷米

我建议如下:

只需获取第一个 DDL 中的 Cursus_ID 并为另一个 DDL 调用更新方法:

void Input_DropDownList_SelectCourse_SelectedItemChanged(
    object sender, 
    BootstrapUI.UserControls.BootstrapFormControls.DropDownListItemEventArgs e)
{
    int Cursus_ID = Convert.ToInt16((sender as Input_DropDownList_SelectCourse).Value);
    Update_Input_DropDownList_SelectPeriod(Cursus_ID);
}

使用辅助方法更新另一个 DDL:

void Update_Input_DropDownList_SelectPeriod(int Cursus_ID)
{
    List<DropdownDisplay> dropdownDisplayList = GetDropdownDisplayList(Cursus_ID);
    Input_DropDownList_SelectPeriod.DataSource = dropdownDisplayList;
    Input_DropDownList_SelectPeriod.DataValueField = "Value";
    Input_DropDownList_SelectPeriod.DataTextField = "Text";
    Input_DropDownList_SelectPeriod.DataBind();
}

使用另一个辅助方法获取另一个 DDL 的数据:

List<DropdownDisplay> GetDropdownDisplayList(Int16 Cursus_ID)
{
    Data.languagesEntities context = Data._languagesEntities.GetDbContext();

    var ListEntities =
        from ent1 in context.Cursusdata
        from ent2 in context.Cursus
        where ent1.Cursus_ID == Cursus_ID
        && ent1.Cursus_ID == ent2.Cursus_ID
        && ent1.Cursusdata_gesloten == 0
        orderby ent1.Cursusdata_startdatum ascending
        select new
        {
            Cursusdata_ID = ent1.Cursusdata_ID,
            Cursus_Code = ent2.Cursus_Code,
            Cursusdata_einddatum = ent1.Cursusdata_einddatum,
            Cursusdata_startdatum = ent1.Cursusdata_startdatum,
            Cursusdata_tijden_NL = ent1.Cursusdata_tijden_NL,
            Cursusdata_tijden_UK = ent1.Cursusdata_tijden_UK,
            Cursus_ID = ent1.Cursus_ID
        };

    List<DropdownDisplay> List_DropdownDisplay = new List<DropdownDisplay>();
    foreach (var Entity in ListEntities.ToList())
    {
        string newStartDatum = (Entity.Cursusdata_startdatum == null) ? "" : ((DateTime)Entity.Cursusdata_startdatum).ToShortDateString();
        string newEindDatum = (Entity.Cursusdata_einddatum == null) ? "" : ((DateTime)Entity.Cursusdata_einddatum).ToShortDateString();

        List_DropdownDisplay.Add(new DropdownDisplay()
        {
            value = Entity.Cursusdata_ID.ToString(),
            Text =
                Entity.Cursus_Code + " - " + newStartDatum +
                " - " + newEindDatum + " - " + Entity.Cursusdata_tijden_NL
        });
    }
    return List_DropdownDisplay;
}

编辑:

在较短版本的查询中,您可以立即创建 DropdownDisplay 对象,而无需中间匿名对象:

List<DropdownDisplay> GetDropdownDisplayList(Int16 Cursus_ID)
{
    Data.languagesEntities context = Data._languagesEntities.GetDbContext();

    var ListEntities =
        from ent1 in context.Cursusdata
        from ent2 in context.Cursus
        where 
            ent1.Cursus_ID == Cursus_ID
            && ent1.Cursus_ID == ent2.Cursus_ID
            && ent1.Cursusdata_gesloten == 0
        orderby ent1.Cursusdata_startdatum ascending            
        select new DropdownDisplay
        {
            value = ent1.Cursusdata_ID.ToString(),
            Text = String.Format(
                "{0}-{1}-{2}-{3}",
                ent2.Cursus_Code,
                ent1.Cursusdata_startdatum == null ? string.Empty : ((DateTime)ent1.Cursusdata_startdatum).ToShortDateString(),
                ent1.Cursusdata_einddatum == null ? string.Empty : ((DateTime)ent1.Cursusdata_einddatum).ToShortDateString(),
                ent1.Cursusdata_tijden_NL
            )
        };

    return ListEntities.ToList();
}

但仍然是 最酷 的方法是让 DropdownDisplay class 根据给定的数据为其属性创建值:

public class DropdownDisplay
{
    public DropdownDisplay(EntityType1 ent1, EntityType2 ent2)
    {
        value = ent1.Cursusdata_ID.ToString();
        Text = String.Format(
            "{0}-{1}-{2}-{3}",
            ent2.Cursus_Code,
            ent1.Cursusdata_startdatum == null ? string.Empty : ((DateTime)ent1.Cursusdata_startdatum).ToShortDateString(),
            ent1.Cursusdata_einddatum == null ? string.Empty : ((DateTime)ent1.Cursusdata_einddatum).ToShortDateString(),
            ent1.Cursusdata_tijden_NL
        );
    }
    public string value { get; set; }
    public string Text { get; set; }
}

那么查询会更短:

List<DropdownDisplay> GetDropdownDisplayList(Int16 Cursus_ID)
{
    Data.languagesEntities context = Data._languagesEntities.GetDbContext();

    var ListEntities =
        from ent1 in context.Cursusdata
        from ent2 in context.Cursus
        where 
            ent1.Cursus_ID == Cursus_ID
            && ent1.Cursus_ID == ent2.Cursus_ID
            && ent1.Cursusdata_gesloten == 0
        orderby ent1.Cursusdata_startdatum ascending            
        select new DropdownDisplay(ent1, ent2);

    return ListEntities.ToList();
}