gpt4 book ai didi

c# - LINQ2SQL : how to merge two columns from the same table into a single list

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:38 28 4
gpt4 key购买 nike

这可能是一个简单的问题,但我只是一个初学者所以...

假设我有一个包含某些人使用的家庭工作地点(城市)的表格。类似于:ID(int)、namePerson(string)、homeLocation(string)、workLocation(string),其中 homeLocation 和 workLocation 都可以为 null。

现在我想将所有使用的不同位置合并到一个列表中。像这样的东西:

var homeLocation =
from hm in Places
where hm.Home != null
select hm.Home;

var workLocation =
from wk in Places
where wk.Work != null
select wk.Work;

List<string> locationList = new List<string>();
locationList = homeLocation.Distinct().ToList<string>();
locationList.AddRange(workLocation.Distinct().ToList<string>());

(我想如果它们在两列中具有相同的值,我想仍然允许重复,我真的不想要......)

我的问题:如何将其放入单个 LINQ 语句中?

预先感谢您的帮助!

最佳答案

var all = homeLocation.Union(workLocation).ToList();

关于c# - LINQ2SQL : how to merge two columns from the same table into a single list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254976/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com