0

I am new to LINQ and have become stumped on the translating the following code. I am currently writing a front end for a SQL database. I have a SQL view that gives a list of identity values (electrolyte_id). The list is grouped by the electrolyte identity value and filtered by the maximum date for each process tanks identity value (process_tanks_id) in the electrolyte_max_date (view). below is the sql query.

SELECT electrolyte.electrolyte_id, electrolyte.process_tanks_id, electrolyte.date_active
FROM   electrolyte_max_date INNER JOIN electrolyte ON 
           electrolyte_max_date.max_date = dbo.electrolyte.date_active AND 
           electrolyte_max_date.process_tanks_id = electrolyte.process_tanks_id

I have been able to translate the electrolyte_max_date query in LINQ. Below is the code:

var filter_electrolyte_list = from tbl_electrolyte in _ds.electrolyte.AsEnumerable()
                              group tbl_electrolyte by
                                     tbl_electrolyte.process_tanks_id into tankgroup
                              select new
                              {
                                  tank = tankgroup.Key,
                                  maxdate = tankgroup.Max(tbl_electrolyte =>
                                                        tbl_electrolyte.date_active)
                               };

My question is: How do I join the filter_electrolyte_list LINQ query to the electrolyte table in another LINQ query? I need to replicate the same results that i get in the SQL View.

Thanks in advance for any help. Jonathan

  • This might be helpful. Join on multiple columns: http://stackoverflow.com/questions/3408462/linq-join-where/3411046#3411046 – Amy B May 16 '12 at 15:28

1 Answers1

0

I feel like what you want to do is Union two Linq Queries?

var filter_electrolyte_list = (from tbl_electrolyte in _ds.electrolyte.AsEnumerable()
                          group tbl_electrolyte by
                                 tbl_electrolyte.process_tanks_id into tankgroup
                          select new
                          {
                              tank = tankgroup.Key,
                              maxdate = tankgroup.Max(tbl_electrolyte =>
                                                    tbl_electrolyte.date_active)
                           }).Union( //New Linq Query that has the same select statement);

Make sure the selected variables in each query match just like a regular SQL Union.

Blast_dan
  • 1,135
  • 9
  • 18