简体   繁体   中英

Taking union of two lists of IEnumerable and IQueryable

Hi I have two tables tabl1 and table2. I am trying search the each table for certain date once i get lists c1 and c2 then I need to get union of the two, so I have one distinct list with no repeating rows using entity framework.

  ssEntities sss = new ssEntities();
  var c1 = sss.table1.ToList().Where(x => x.date >= DateTime.Today);
  var c2=sss.table2.ToList().Where(x=>x.date1 DateTime.Today);
  var ll=c1.Union(c2).ToList();

When I try to take union of the two lists I get error given below

Instance argument: cannot convert from 
'System.Collections.Generic.IEnumerable<testproject.table1>' to   'System.Linq.IQueryable<testproject.table2>'

Please help.

That's easy enough

ssEntities sss = new ssEntities();
  var c1 = sss.table1.ToList().Where(x => x.date >= DateTime.Today)
                            .Select(x => new { x.Prop1, x.Prop2 });
  var c2 = sss.table2.ToList().Where(x => x.date1 >= DateTime.Today)
                            .Select(x => new { x.Prop1, x.Prop2 });
  var ll = c1.Union(c2).ToList();

You have to only select the properties you want from each table , and they have to be the same, so that the union will work, you can't do a Union on 2 different types.

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