C# 在Linq中执行一个值数组,以返回MVC控制器中的特定数据

C# 在Linq中执行一个值数组,以返回MVC控制器中的特定数据,c#,arrays,linq,C#,Arrays,Linq,背景: 我正在使用LINQtoSQL类来连接我的数据库 我已经建立了MyProjectRepo.cs 所以问题是: 这个foreach循环只执行从ajax传递的数组的第一个值。例如:Ajax在数组中返回3个值(functionId{1,3,5}),当我在控制器中放置断点时,我可以在foreach(functionId{1,3,5}中的int I)中看到这个数组,但它只执行一次(I=1)。如何使该控制器工作,使其能够执行数组中的每个值以获取所需的数据 我这样做的原因是,有时这些functionID

背景:

我正在使用LINQtoSQL类来连接我的数据库

我已经建立了MyProjectRepo.cs

所以问题是:

这个
foreach
循环只执行从ajax传递的数组的第一个值。例如:Ajax在数组中返回3个值(functionId{1,3,5}),当我在控制器中放置断点时,我可以在
foreach(functionId{1,3,5}中的int I)
中看到这个数组,但它只执行一次(I=1)。如何使该控制器工作,使其能够执行数组中的每个值以获取所需的数据

我这样做的原因是,有时这些functionID有一些相同的主题名称,我不希望multiselectlist有那些重复的名称

例如:

{functionId=1主题\u name=topic1、topic2、topic3}

{functionId=2主题\u name=topic2 topic3 topic4}

{functionId=3主题\u name=topic1、topic3、topic5}

如果用户选择functionId{1,2,3},我希望看到的结果是{topic_name=topic1,2,3,4,5}而不是{topic_name=1,1,2,2,3,3,3,4,5}

我知道这在SQL中很容易实现,只需在(1,2,3)中键入
where functionId,但由于我在MVC中使用Linq和JQueryAJAX,我想在存储库中进行匹配和比较函数比在
MyProjectRepo.cs
中编写复杂的Linq更好。实际上,这没有帮助,因为模型不直接与Ajax对话

以下是我的逻辑:Linq将整个表重新构建到我的模型中,并将其传递到
\u repository.GetAllTopicById
,然后我在controller中声明
var alltopic=this\u repository.GetAllTopicById
,然后我可以将从Ajax收集的ID与此控制器匹配

我希望我写的整个序列能够被每个人理解,如果有人有任何解决方案来完成这项工作,或者对我写的某些部分有任何疑问,请在下面写一行。任何回复都将不胜感激

谢谢大家!


Kevin

而不是使用for each。你能试试这个代码吗

[Test]
    public void ArrayValueInLinq()
    {
        int[] functionId=new int[]{1,2};
        IList<SQLTable> alltopic=new List<SQLTable>
        {
            new SQLTable
            {
               topic_id=1,
               topic_name="john"
            },
            new SQLTable
            {
               topic_id=1,
               topic_name="john"
            },
            new SQLTable
            {
               topic_id=2,
               topic_name="Tomal"
            },
            new SQLTable
            {
               topic_id=1,
               topic_name="john"
            }
        };
        var result = (from at in alltopic
                      select new
                      {
                          id = at.topic_id,
                          topicname = at.topic_name
                      }).Where(p=>functionId.Contains(p.id)).Distinct().ToList();
    }
    public class SQLTable
    {
        public int topic_id { get; set; }
        public string topic_name { get; set; }
    }
[测试]
public void ArrayValueInLinq()
{
int[]functionId=新的int[]{1,2};
IList alltopic=新列表
{
新SQLTable
{
主题id=1,
topic_name=“john”
},
新SQLTable
{
主题id=1,
topic_name=“john”
},
新SQLTable
{
主题id=2,
topic_name=“Tomal”
},
新SQLTable
{
主题id=1,
topic_name=“john”
}
};
var result=(来自alltopic中的at)
选择新的
{
id=at.topic\u id,
topicname=at.topic\u name
}).Where(p=>functionId.Contains(p.id)).Distinct().ToList();
}
公共类SQLTable
{
公共int主题_id{get;set;}
公共字符串主题_name{get;set;}
}

为什么要从for each中返回一个值?使用不同的方法消除重复项。哇,伙计,你真的解决了我一周来一直困惑的问题。我把你的
.Where(p=>functionId.Contains(p.id))
取下来,因为我需要让
p.id
等于
function\u id
,所以我声明一个
fid=at.function\u id
并将你的代码传输到
。Where(p=>functionId.Contains(p.fid))
。这就行了!非常感谢你为我解决了这个问题!我还有一个关于
.Where()
子句的问题。如果我想要
.Where()
中的多个条件,例如
.Where(p=>functionId.Contains(p.fid)和&t=>topicId.Contains(t.tid))
,我如何编写?但是函数似乎不像我使用“&&”或“| |”的方式。您可以像这样使用p=>functionId.Contains(p.id)&&p.id==30
IList<SQLTable> GetAllTopicById(int functionId);
public MyProjectModel()
  {
   AvailableFunction = new List<MultiSelectList>();
   AvailableTopic = new List<MultiSelectList>();
  }
  // 1st List box that works properly 
   public IList<MultiSelectList> AvailableFunction{get;set;}
  // 2nd list box that is related with this Question
   public IList<MultiSelectList> AvailableTopic{get;set;}
    public ActionResult GetAllTopic(int[] functionId)
      {
        foreach(int i in functionId) 
        {
         var alltopic = _repository.getAllTopicById(i)
         var result = (from at in alltopic
                       where at.function_id = i
                       select new
                       {
                        id= at.topic_id,
                        topicname = at.topic_name
                       }).Distinct().ToList();
    return Json(result, JsonRequestBehavior.AllowGet);
        }
    return View();// If I don't write this return, it will say "not all code paths return a
          // value" Is this because I wrote everything in that Foreach loop?
         }
[Test]
    public void ArrayValueInLinq()
    {
        int[] functionId=new int[]{1,2};
        IList<SQLTable> alltopic=new List<SQLTable>
        {
            new SQLTable
            {
               topic_id=1,
               topic_name="john"
            },
            new SQLTable
            {
               topic_id=1,
               topic_name="john"
            },
            new SQLTable
            {
               topic_id=2,
               topic_name="Tomal"
            },
            new SQLTable
            {
               topic_id=1,
               topic_name="john"
            }
        };
        var result = (from at in alltopic
                      select new
                      {
                          id = at.topic_id,
                          topicname = at.topic_name
                      }).Where(p=>functionId.Contains(p.id)).Distinct().ToList();
    }
    public class SQLTable
    {
        public int topic_id { get; set; }
        public string topic_name { get; set; }
    }