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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9103537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:57:19  来源:igfitidea点击:

Select all columns on an object with Linq

c#linqlinq-to-sql

提问by Isaiah Nelson

I have a query:

我有一个疑问:

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

But in reality, I need to avoid using an anonymous type because it is readonly and select all the properties/columns on my "transaction" item.

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

I would have thought that it would be something like this:

我会认为它会是这样的:

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

Or...

或者...

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

Is there no way to retrieve everything the object has for properties and pass it to the Ienumerable?

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

采纳答案by Steve Danner

If you want to avoid anonymous types and get everything, why not just return an IEnumerableof the original transaction item?

如果您想避免匿名类型并获取所有内容,为什么不只返回IEnumerable原始交易项目的一个?

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

回答by Yahia

try

尝试

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

回答by Chris Klepeis

I believe this would work.

我相信这会奏效。

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

回答by deltree

I think you want

我想你想要

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

or

或者

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

if you truly just want an IEnumerable

如果你真的只想要一个 IEnumerable

回答by usr

Why do you "need" to avoid an anonymous type here?

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

You have the following options:

您有以下选择:

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 }

The last one is inconvenient to handle. No way around this. Tell us what you want to do.

最后一个不方便处理。没有办法解决这个问题。告诉我们你想做什么。

回答by fiberOptics

For a single return value you could use:

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

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

For an IEnumerable return:

对于 IEnumerable 返回:

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

回答by Dani Mathew

In addition, If there is a joincondition between objects, we might get the result using..

此外,如果对象之间存在连接条件,我们可能会使用..

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

回答by Tauseef Nabiji

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();