簡體   English   中英

LINQ 從 class 返回屬性

[英]LINQ To Return Properties from a class

首先,XAML 和一般編程的新手。 我正在嘗試在 .cs 文件中填充 XAML Datagrid。

我使用的每個 SQL 表都有類,包含引用列的屬性

    public partial class TTDepartment
    {
        public TTDepartment()
        {
            this.TTEventLogs = new HashSet<TTEventLog>();
            this.TTUserDepartments = new HashSet<TTUserDepartment>();
        }

        public string Department { get; set; }
        public string Manager { get; set; }
        public string ManagerEmail { get; set; }

        public virtual ICollection<TTEventLog> TTEventLogs { get; set; }
        public virtual ICollection<TTUserDepartment> TTUserDepartments { get; set; }
    }

我目前正在做的是嘗試在我的類上使用 LINQ 查詢來檢索數據,就像這樣

(我知道可能有更好的方法來做到這一點)請幫忙

        private void GetDepartments()
        {

            using (var context = new CetusEntities())// New instance of Database class
            {
                var linqDept = context.TTDepartments.Where(s => s.Department != "" || s.Department != null); // Should return All Departments from TTDepartments Class?
                //Populate colDept in Datagrid with results from LINQ
            } 
        }

這是用於 Datagrid 的 XAML

                <DataGrid x:Name="dgDept"  Height="200">
                    <DataGrid.Columns>
                        <DataGridTextColumn x:Name="colDept" Binding="{Binding Source = {StaticResource Department}}" Header="Department"></DataGridTextColumn>
                        <DataGridTextColumn x:Name="colMan" Binding="{Binding Source = {StaticResource Department}}" Header="Manager"></DataGridTextColumn>
                        <!--<DataGridTextColumn Header="Email"></DataGridTextColumn>-->
                    </DataGrid.Columns>
                </DataGrid>

您的查詢應如下所示:

var linqDept = context.TTDepartments.Where(s =>(!string.IsNullOrEmpty(s)));

另外,驗證您的數據庫中是否確實有數據。 然后調試

//將數據網格源設置為測試。

dtGrid.ItemsSource = linqDept;

XAML 示例:

<DataGridTextColumn x:Name="colDept" Binding="{Binding Department}" Header="Department"></DataGridTextColumn>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM