C# Question

Linq query to get result of query as array of int

This is my table.

grp_perm_id grp_id Perm_id
23 2 2
27 2 1
28 3 1
29 2 3

I want to retrieve results in the form of array of integers. Below is the code I am trying

public List<int> GetData(int grp_id)
// List<ts_grp_perm_mapping> tm = (from c in db.ts_grp_perm_mapping where c.grp_id == grp_id select c).ToList();
int[] selectedradiobuttons = (from c in db.ts_grp_perm_mapping where c.grp_id == grp_id select c).ToArray();
return selectedradiobuttons.ToArray();

Here I am receiving grp_id. For example if I receive 2(grp_id) then I want to return array of integers which contains perm_id (2,1,3). If the grp_id is 3 then array of integers contains perm_id(1). How can I do this? In the above code I am getting this error:

cannot implicitly convert type c3card.dal.edmodel.ts_grP_perm[] to int

Can anybody suggest me how to do this? I want to return array of integers.

This is my json code.

public JsonResult GetCheckedPerm(int usr_groupId)
List<int> selectedradio = GrpPermBAL.GetData(usr_groupId);


Answer Source

You're currently selecting the whole row, rather than just the perm_id. You're also calling ToArray twice, and trying to return an array despite your method's return type being List<int>. I'd just use:

public List<int> GetData(int groupId)
    return db.ts_grp_perm_mapping
             .Where(c => c.grp_id == groupId)
             .Select(c => c.Perm_id)

I've adjusted the name of the parameter to be more conventional - I'd strongly recommend that you adjust the property names (ts_grp_perm_mapping, grp_id, Perm_id) to be more conventional too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download