C# 在linq查询中对选定的可枚举项进行筛选

C# 在linq查询中对选定的可枚举项进行筛选,c#,linq-to-objects,C#,Linq To Objects,是否可以将下面的linq查询合并到一个查询中 var checkBoxes = from x in FindAll<CheckBox>() where x.Checked select new { RecordType = Type.GetType(x.Attributes["RecordType"]),

是否可以将下面的linq查询合并到一个查询中

var checkBoxes = from x in FindAll<CheckBox>()
                 where x.Checked
                 select new
                 {
                     RecordType = Type.GetType(x.Attributes["RecordType"]),
                     RecordId = Int32.Parse(x.Attributes["RecordId"])
                 };

checkBoxes = from x in checkBoxes
             where x.RecordType != typeof(DomainModel.Group)
             select x;
var复选框=来自FindAll()中的x
x.在哪里检查
选择新的
{
RecordType=Type.GetType(x.Attributes[“RecordType”]),
RecordId=Int32.Parse(x.Attributes[“RecordId”])
};
复选框=从复选框中的x开始
其中x.RecordType!=typeof(DomainModel.Group)
选择x;
var复选框=来自FindAll()中的x
让recordType=Type.GetType(x.Attributes[“recordType”])
其中x.已选中&&recordType!=typeof(DomainModel.Group)
选择新的
{
记录类型=记录类型,
RecordId=Int32.Parse(x.Attributes[“RecordId”])
};
var复选框=来自FindAll()中的x
让recordType=Type.GetType(x.Attributes[“recordType”])
其中x.已选中&&recordType!=typeof(DomainModel.Group)
选择新的
{
记录类型=记录类型,
RecordId=Int32.Parse(x.Attributes[“RecordId”])
};
var复选框=FindAll()
.Where(x=>x.Checked&&Type.GetType(x.Attributes[“RecordType”])!=typeof(DomainModel.Group))
.选择(新建){
RecordType=Type.GetType(x.Attributes[“RecordType”]),
RecordId=Int32.Parse(x.Attributes[“RecordId”])
});
var复选框=FindAll()
.Where(x=>x.Checked&&Type.GetType(x.Attributes[“RecordType”])!=typeof(DomainModel.Group))
.选择(新建){
RecordType=Type.GetType(x.Attributes[“RecordType”]),
RecordId=Int32.Parse(x.Attributes[“RecordId”])
});

LasseSpeholt的答案非常好(更可取,甚至-如果要丢弃结果,则进行投影没有意义),但如果您想更普遍地应用这一点,可以使用查询继续:


LasseSpeholt的答案非常好(更可取,甚至-如果要放弃结果,进行投影没有任何意义),但如果您想更普遍地应用这一点,可以使用查询继续:


为什么要更改为一个linq查询?Linq使用延迟执行,并且仅当您实际在某处使用输出时才会执行。同时在不断建立表达式树。你所拥有的是完全可读的。只有当您认为它更具可读性时,我才会更改它。

为什么要更改为一个linq查询?Linq使用延迟执行,并且仅当您实际在某处使用输出时才会执行。同时在不断建立表达式树。你所拥有的是完全可读的。只有当您认为它更可读时,我才会更改它。

+1使用let关键字是个好主意。顺便问一下,我们可以在通用linq表达式中使用诸如“let”关键字之类的方法吗?看看我的答案。@Danny是的,你可以把它包括在一个选择中。exEnumerable.Range(1,10)。选择(i=>new{i,d=i*2})。其中(x=>x.d==something)。选择(x=>x.i);其中d是我可以放在“let”中的。(这是一个非常简单的例子)现在有两个非常可以接受的答案。我认为这是第一个。。。我希望这不是对SO ethicetta的反对,并且每个人都认为这是公平的:)+1使用let关键字是一个好主意。顺便问一下,我们可以在通用linq表达式中使用诸如“let”关键字之类的方法吗?看看我的答案。@Danny是的,你可以把它包括在一个选择中。exEnumerable.Range(1,10)。选择(i=>new{i,d=i*2})。其中(x=>x.d==something)。选择(x=>x.i);其中d是我可以放在“let”中的。(这是一个非常简单的例子)现在有两个非常可以接受的答案。我认为这是第一个。。。我希望这不是对SO ethicetta的反对,并且每个人都认为这是公平的:)+1代表“选择……进入……”。就我个人而言,我认为在where子句中没有“let”更漂亮。@LasseSpeholt:在where子句中有两个where子句和一个let子句,都是独立的。很抱歉我的英语不正确。我是说inside=between:)我把拉塞斯珀霍尔特的答案标记为接受,但是+1,谢谢你对这个主题的非常好的阐述@拉塞斯珀霍尔特:好的。。。当然,您可以将
let
放在复合词
where
之前,但这意味着会有更多的调用
Type.GetType
+1来表示“select…into…”。就我个人而言,我认为在where子句中没有“let”更漂亮。@LasseSpeholt:在where子句中有两个where子句和一个let子句,都是独立的。很抱歉我的英语不正确。我是说inside=between:)我把拉塞斯珀霍尔特的答案标记为接受,但是+1,谢谢你对这个主题的非常好的阐述@拉塞斯珀霍尔特:好的。。。当然,您可以将
let
放在复合
where
之前,但这意味着需要更多调用
Type.GetType
var checkBoxes = from x in FindAll<CheckBox>()
                 let recordType = Type.GetType(x.Attributes["RecordType"])
                 where x.Checked && recordType != typeof(DomainModel.Group)
                 select new
                 {
                     RecordType = recordType,
                     RecordId = Int32.Parse(x.Attributes["RecordId"])
                 };
var checkboxes = FindAll<CheckBox>()
                  .Where(x => x.Checked && Type.GetType(x.Attributes["RecordType"]) != typeof(DomainModel.Group))
                  .Select(new{
                          RecordType = Type.GetType(x.Attributes["RecordType"]),
                          RecordId = Int32.Parse(x.Attributes["RecordId"])
                  });
var checkBoxes = from x in FindAll<CheckBox>()
                 where x.Checked
                 select new
                 {
                     RecordType = Type.GetType(x.Attributes["RecordType"]),
                     RecordId = Int32.Parse(x.Attributes["RecordId"])
                 } into y
                 where y.RecordType != typeof(DomainModel.Group)
                 select y;
var checkBoxes = from x in FindAll<CheckBox>()
                 where x.Checked
                 let t = Type.GetType(x.Attributes["RecordType]")
                 where t != typeof(DomainModel.Group)
                 select new
                 {
                     RecordType = t
                     RecordId = Int32.Parse(x.Attributes["RecordId"])
                 };