xainu31
xainu31

Reputation: 70

Join Linq Entity With Datatable

I want to join Data Table with a linq entity. Here is the code hope fully u will understand what i am trying to do here with the code

var db = new TempR_ClientRCEntities();
DataTable dbc = GetAllSites();
List<object> temp = dbc.AsEnumerable().ToList<object>();

var thermoTypes = from type in db.TempR_ThermometerType
                  join site in dbc on type.SiteID equals dbc.SiteId
                  select type;

Upvotes: 1

Views: 2100

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236288

You can use join, but that will require loading all thermometer types to client (assume site id has integer type):

var query = from t in db.TempR_ThermometerType.AsEnumerable()
            join r in dbc.AsEnumerable()
                 on t.SiteID equals r.Field<int>("SiteId") 
            select t;

Or you can use server-side filtering of thermometer types, but in that case you need to pass site ids to server - it will generate SQL operator IN):

var ids = dbc.AsEnumerable().Select(r => r.Field<int>("SiteId")).Distinct();
var query = from t in db.TempR_ThermometerType
            where ids.Contains(t.SiteID)
            select t; 

Upvotes: 2

Related Questions