LINQ query with Distinct and Union

前端 未结 4 1569
[愿得一人]
[愿得一人] 2021-01-01 17:29

I currently have 2 queries that are returning lists of MyModel like this:

var q1 = ....
         select new MyModel()
         {
             TheData1 = ...
         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 17:57

    Union creates an Enumerable with unique values from both collections. In other words, you don't need Distinct.

    edit: example of Union here

    edit2: forgot that it's not the list of UniqueIDs that you're concatenating. I removed the suggested code since it was wrong. You should be able to do a simple Union if you implement an IEqualityComparer, but that might be overkill.

提交回复
热议问题