Linq 查询仅返回数组的第一项

发布于 2024-12-13 23:52:39 字数 666 浏览 0 评论 0原文

让字符串项目等于“p1,p2,p3”,并且在数据库中每个项目的标识符存在如下 p1 = 1 ,p2 = 2, p3 = 3 。在我的查询中仅返回列表中的第一个项目,任何为什么?

private List<int> getProjects(string projects)
    {

        String[] projectArray = projects.Split(',');

        QMOIIEntities db = new QMOIIEntities();

        var projectList = db.wbs_projects
          .Where(x => projectArray.Contains(x.prjName))
          .Select(x => x.prjID).ToList();

        return projectList;
    }

**更新 字符串中的空格

String[] projectArray = projects.Split(',');

这个问题是我更改为的

 String[] projectArray = projects.Trim().Split(',');

Let the string projects equal "p1,p2,p3" and in the database the identifier for each exists as follows p1 = 1 ,p2 = 2, p3 = 3. Only the first project in the list is being returned in my query, any thoughts on why?

private List<int> getProjects(string projects)
    {

        String[] projectArray = projects.Split(',');

        QMOIIEntities db = new QMOIIEntities();

        var projectList = db.wbs_projects
          .Where(x => projectArray.Contains(x.prjName))
          .Select(x => x.prjID).ToList();

        return projectList;
    }

**UPDATE
This issue was whitespace in the string I changed

String[] projectArray = projects.Split(',');

to

 String[] projectArray = projects.Trim().Split(',');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-12-20 23:52:39

以下代码使用与您在问题中提到的相同逻辑。我创建了虚拟数据。该代码的行为符合预期,即输出是与每个 prjName 对应的 prjID。

void Main()
{
    string projects = "p1,p2,p3";
    List<string> projectArray = projects.Split(',').ToList();

    TestEntities db = new TestEntities();
    db.wbs_projects = new List<TestEntities>();
    db.wbs_projects.Add(new TestEntities(){prjName = "p1",prjID="Test1"});
    db.wbs_projects.Add(new TestEntities(){prjName = "p2",prjID="Test2"});
    db.wbs_projects.Add(new TestEntities(){prjName = "p3",prjID="Test3"});

    var projectList = db.wbs_projects
      .Where(x => projectArray.Contains(x.prjName))
      .Select(x => x.prjID).ToList();

    foreach(var item in projectList)
    {
        Console.WriteLine(item);//Test1,Test2,Test3
    }

}

public class TestEntities
{   
    public List<TestEntities> wbs_projects{get;set;}

    public string prjName{get;set;}
    public string prjID{get;set;}
}

The following code uses the same logic, the one you have mentioned in your question. I have created dummy data. The code is behaving as expected i.e. the output is the prjID corresponding to each prjName.

void Main()
{
    string projects = "p1,p2,p3";
    List<string> projectArray = projects.Split(',').ToList();

    TestEntities db = new TestEntities();
    db.wbs_projects = new List<TestEntities>();
    db.wbs_projects.Add(new TestEntities(){prjName = "p1",prjID="Test1"});
    db.wbs_projects.Add(new TestEntities(){prjName = "p2",prjID="Test2"});
    db.wbs_projects.Add(new TestEntities(){prjName = "p3",prjID="Test3"});

    var projectList = db.wbs_projects
      .Where(x => projectArray.Contains(x.prjName))
      .Select(x => x.prjID).ToList();

    foreach(var item in projectList)
    {
        Console.WriteLine(item);//Test1,Test2,Test3
    }

}

public class TestEntities
{   
    public List<TestEntities> wbs_projects{get;set;}

    public string prjName{get;set;}
    public string prjID{get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文