Linq 查询仅返回数组的第一项
让字符串项目等于“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下代码使用与您在问题中提到的相同逻辑。我创建了虚拟数据。该代码的行为符合预期,即输出是与每个 prjName 对应的 prjID。
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.