简体   繁体   English

如何使用LINQ将多个字段组合到一个字段中

[英]How to combine multiple fields into one field using LINQ

I have an Entity table with 2 int fields and want to get all the values into a list of ints not a list of the combinations of ints 我有一个包含2个int字段的Entity表,并希望将所有值都放入一个int列表中,而不是一个int组合列表

This doesn't seem to give a list of single ints 这似乎没有列出单个整数

var allItems = (from tbl1 in objContext.MyTable
select new { tbl1.Field1, tbl1.Field2 }).ToList();

How can I achieve this? 我怎样才能做到这一点?

You should be able to use: 你应该可以使用:

var allItems = objContext.MyTable
                .SelectMany(t => new[] { t.Field1, t.Field2 })
                .ToList();

The first select creates an array from the two elements, then the SelectMany flattens that into a single enumerable. 第一个选择从两个元素创建一个数组,然后SelectMany平为一个可枚举的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM