Linq查询与ToList()与不

时间:2019-10-23 16:40:12

标签: c# linq

我实际上是LINQ的新手,并具有以下查询:

var deletes = (from MDM in mdmList
                       let status = "Deleted"
                       let mdm_id = MDM.brandId
                       where !(from Bdb in bdbList
                               let bdb_id = Bdb.brandId
                               select bdb_id)
                             .Contains(mdm_id)
                       &&
                            !string.IsNullOrEmpty(MDM.brandId)
                       select new
                       {
                           MDM.brandId,
                           MDM.brandName,
                           MDM.brandOEMFlag,
                           MDM.brandOwnerId,
                           MDM.brandOwner,
                           MDM.parentId,
                           MDM.parentCompany,
                           MDM.subBrandId,
                           MDM.subBrand,
                           MDM.systemBrandID,
                           MDM.systemSubBrandID,
                           status
                       }).ToList();

然后,我使用以下方法将var deletes转换为DataTable

public static DataTable ToDataTable<BdbLayout>(List<BdbLayout> items)
    {
        DataTable dataTable = new DataTable(typeof(BdbLayout).Name);

        //Get all the properties
        PropertyInfo[] Props = typeof(BdbLayout).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Defining type of data column gives proper data table 
            var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name, type);
        }
        foreach (BdbLayout item in items)
        {
            var values = new object[Props.Length];
            for (int i = 0; i < Props.Length; i++)
            {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }
        return dataTable;
    }

一切正常,但是如果我从查询中删除.ToList(),则会注意到对性能的巨大影响。

所以,我的问题是,当您不指定列表转换时,如何存储var deletes?为什么对应用程序的运行时间有很大影响?

预先感谢,爱大家!

0 个答案:

没有答案