0

I've 2 tables

DeviceType Table

id   Name 
1    Device Type 1
2    Device Type 2

Device Table

id   Name     Device Type Id (fk)
1    Device1  1
2    Device2  1
3    Device3  2

What I want is to query the data from device table with device type name using LINQ Methods. I couldn't find the Include method to use here.

I'm trying to get this in result

id Name     DeviceType Name
1  Device1  Device Type 1
2  Device2  Device Type 1
3  Device3  Device Type 2

3 Answers 3

2
var device = from g in db.Devices
        join m in db.DeviceType on g.DeviceTypeId equals m.Id  
        select new 
        {
          Id = g.Id,
          Name = g.Name,
          DeviceTypeName = m.Name
        };

Or you can try something like

var device = db.Devices
             .Join(db.DeviceType,
                 g => g.Id,      // Device Table Id 
                 m => m.Id,      // DeviceType Table Id
                 (g, m) => new { 
                               Id = g.Id, 
                               Name = g.Name, 
                               DeviceTypeName = m.Name 
              }) 
             .Where(deviceTyp => deviceTyp.g.ID == Id);     
             // where condition for g.DeviceTypeId equals m.Id  
Sign up to request clarification or add additional context in comments.

2 Comments

can this be achieved using linq methods?
db.Devices.Join(db.DeviceType, g => g.DeviceTypeId, m => m.Id, (g, m) => new { Id = g.Id, Name = g.Name, DeviceTypeName = m.Name })
1

You do not need to use .Include unless you want to get the related entities as well. You can do something like this:

context.Devices.Where(your conditions here)
      .Select(d=>new {Id = d.id, Name = d.Name, DeviceTypeName = d.DeviceType.Name})

You do not need to do join since there's a FK relationship (looks like that from the question), EF should pick up the relationship

2 Comments

It is not stated if there is in fact a relationship between the two fields in the database however.
It seems there's a FK, he mentioned it twice
0
List<DeviceType> deviceTypeList = new List<DeviceType>() { 
            new DeviceType { id = 1, Name = "Device Type 1" }, 
            new DeviceType { id = 2, Name = "Device Type 2" } };

List<Device> deviceList = new List<Device>() { 
            new Device { id = 1, Name = "Device1", DeviceTypeId = 1 },
            new Device { id = 2, Name = "Device2", DeviceTypeId = 1 },
            new Device { id = 3, Name = "Device3", DeviceTypeId = 2 } };

//Linq
var query = from device in deviceList
        join type in deviceTypeList
        on device.DeviceTypeId equals type.id
        select new { Id = device.id, Name = device.Name,
        DeviceTypeName = type.Name };


//Or lambda expressions
var query = deviceList.Join(deviceTypeList,
        device => device.DeviceTypeId,
        type => type.id,
        (device, type) => new { Id = device.id, Name = device.Name, 
        DeviceTypeName = type.Name });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.