LINQ2SQL: how to merge two columns from the same table into a single list
Posted
by
TomL
on Stack Overflow
See other posts from Stack Overflow
or by TomL
Published on 2011-11-24T09:45:51Z
Indexed on
2011/11/24
9:52 UTC
Read the original article
Hit count: 242
c#
|linq-to-sql
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!
© Stack Overflow or respective owner