使用Linq选择对象上的所有列

本文关键字:Linq 选择 对象 使用 | 更新日期: 2023-09-27 18:22:14

我有一个查询:

var transactions = from t in db.Transactions
                        where t.SellingPrice != 0 
                        select new { t.CommissionPercent, t.SellingPrice };

但实际上,我需要避免使用匿名类型,因为它是只读的,并选择我的"事务"项上的所有属性/列。

我本以为会是这样的:

var transactions = from t in db.Transactions
                        where t.SellingPrice != 0 
                        select t.SellingPrice, t.CommissionPercent, t.Etc...

或者。。。

var transactions = from t in db.Transactions
                        where t.SellingPrice != 0 
                        select t.SellingPrice
                        select t.CommissionPercent
                        select t.Etc...

难道没有办法检索对象的所有属性并将其传递给Ienumerable吗?

使用Linq选择对象上的所有列

如果您想避免匿名类型并获取所有内容,为什么不返回原始事务项的IEnumerable呢?

var transactions = from t in db.Transactions
                        where t.SellingPrice != 0 
                        select t;

此外,如果对象之间存在join条件,我们可以使用。。

var result = (from t in db.Transactions
                      join te in db.TransactionsEntries 
                             on t.WorkorderID equals te.WorkorderID                         
             select new { t, te }).ToList();

我相信这会奏效。

var transactions = from t in db.Transactions
                        where t.SellingPrice != 0 
                        select t;

我想你想要

var transactions = db.Transactions.Where(t => t.SellingPrice != 0).ToList();

var transactions = db.Transactions.Where(t => t.SellingPrice != 0).AsEnumerable();

如果您真的只是想要一个IEnumerable

尝试

var transactions = from t in db.Transactions
                    where t.SellingPrice != 0 
                    select t;

对于单个返回值,可以使用:

var transactions = (from t in db.Transactions
                    where t.SellingPrice != 0 
                    select t).FirstOrDefault();  

对于IEnumerable返回:

var transactions = (from t in db.Transactions
                    where t.SellingPrice != 0 
                    select t).ToList();  

为什么在这里"需要"避免匿名类型?

您有以下选项:

select t
select new { t.CommissionPercent, t.SellingPrice }; 
select new MyCustomDtoClass { CommissionPercent = t.CommissionPercent, SellingPrice  = t.SellingPrice }; //also ok
select new object[] { t.CommissionPercent, t.SellingPrice }

最后一个不方便处理。没有办法绕过这个。告诉我们您想做什么。

var transactions = (from t in db.Transactions
                        select new
                       {
                           t.SellingPrice,
                           t.CommissionPercent,
                           ...,
                           ...,
                           t.Etc...
                       }).AsEnumerable().Select(x => new HomeModel // Create a model which have following properties
                        {
                           SellingPrice= x.SellingPrice,  //(where SellingPrice is a HomeModel property)
                           AdCategoryTitle = x.CommissionPercent,
                           ...,
                           ...,
                           ETc... = t.Etc...
                       }).ToList();