简体   繁体   中英

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

this is probably a simple question, but I'm only a beginner so...

Suppose I have a table containing home-work locations (cities) certain people use. Something like: ID(int), namePerson(string), homeLocation(string), workLocation(string) where homeLocation and workLocation can both be null.

Now I want all the different locations that are used merged into a single list. Something like:

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

(which I guess would still allow duplicates if they have the same value in both columns, which I don't really want...)

My question: how this be put into a single LINQ statement?

Thanks in advance for your help!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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