简体   繁体   English

Linq查询以int数组形式获取查询结果

[英]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. 我在这里收到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). 例如,如果我收到2(grp_id),那么我想返回包含perm_id(2,1,3)的整数数组。 If the grp_id is 3 then array of integers contains perm_id(1). 如果grp_id为3,则整数数组包含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 无法将类型c3card.dal.edmodel.ts_grP_perm []隐式转换为int

Can anybody suggest me how to do this? 有人可以建议我该怎么做吗? I want to return array of integers. 我想返回整数数组。

This is my json code. 这是我的json代码。

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

    return(selectedradio,JsonRequestBehavior.AllowGet);
}

You're currently selecting the whole row, rather than just the perm_id. 您当前正在选择整行,而不仅仅是perm_id。 You're also calling ToArray twice, and trying to return an array despite your method's return type being List<int> . 您还两次调用ToArray ,并且尽管方法的返回类型为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)
             .ToList();
}

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. 我将参数的名称调整为更加常规-我强烈建议您将属性名称( ts_grp_perm_mappinggrp_idPerm_id )也调整为更加常规。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM