LINQ选择和哪里没有正确返回任何结果?

时间:2013-11-01 18:23:56

标签: c# asp.net-mvc linq lambda

我试图只选择某位受访者所做的某些预审者,但是我的代码却无法这样做。

CODE:

 IEnumerable<PreScreener> testin = new List<PreScreener>();
                for (int i = 0; i < respondent.PreScreenerResponses.Count; i++)
                {
                    testin = project.PreScreeners.Where(x => x.Id == respondent.PreScreenerResponses[i].PreScreenerId);
                }

模型:

public class PreScreener : IHasId, IHasProjectId
    {
        public int? Id { get; set; }

        public int? ProjectId { get; set; }

        public string ExternalId { get; set; }

        public string Name { get; set; }

        public int? PartialCount { get; set; }

        public int? DisqualifiedCount { get; set; }

        public int? CompleteCount { get; set; }

        public DateTime? CreatedOn { get; set; }

        public DateTime? UpdatedOn { get; set; }

        public List<PreScreenerQuestion> Questions { get; set; }
    }



public class PreScreenerResponse : IHasId, IHasProjectId
{
    public int? Id { get; set; }

    public int? ProjectId { get; set; }

    public int? PreScreenerId { get; set; }

    public int? RespondentId { get; set; }

    public string Ip { get; set; }

    public DateTime SubmittedDate { get; set; }

    public DateTime CreatedOn { get; set; }

    public double? Latitude { get; set; }

    public double? Longitude { get; set; }

    public Dictionary<short, PreScreenerQuestionResponse> Response { get; set; }
}

Respondent.PreScreenerResponses [i]&lt; - Prescreeners对Prescreeners的回应 Project.PreScreeners [i]&lt; - 所有属于该项目的Prescreeners

我只是想把所有Prescreeners都拿到列表中,其中也包括受访者的回复。我想我会创建一个新的List然后只是组合两个不同的列表,但是我的选择Where not not working我总是得到一个空的testin列表。

基于响应的编辑代码:

我试过这个无济于事

        IEnumerable<PreScreener> testin = new List<PreScreener>();
        testin = project.PreScreeners.Where(p => respondent.PreScreenerResponses
                                                            .Select(r => r.PreScreenerId)
                                                                .Contains(p.Id));

有效的答案在下方检查!!!

4 个答案:

答案 0 :(得分:3)

您的for loop是不必要的。只需使用:

testin = project.PreScreeners.Where(x => respondent.PreScreenerResponses.Any(psr => psr.PreScreenerId == x.Id)).ToList();

答案 1 :(得分:1)

for (int i = 0; i < respondent.PreScreenerResponses.Count; i++)
{
    testin = project.PreScreeners.Where(x => x.Id == respondent.PreScreenerResponses[i].PreScreenerId);
}

这是在每个循环上覆盖 testin

{进一步分析以提供实际上有用的东西}

我想你想要:

project.PreScreeners
       .Where(p => respondent.PreScreenerResponses
                             .Select(r => r.PreScreenerId)
                             .Contains(p.Id)
             )

或altrenately

project.PreScreeners
       .Join(respondent.PreScreenerResponses,
             p => p.Id
             r => r.PreScreenerId
            )
       .Select((p, r) => p);   // select just the PreScreener

答案 2 :(得分:0)

每次迭代for循环时,都会重新分配列表。我想你想这样做:

IEnumerable<PreScreener> testin = new List<PreScreener>();
for (int i = 0; i < respondent.PreScreenerResponses.Count; i++)
{
    testin.AddRange(project.PreScreeners.Where(x => x.Id == respondent.PreScreenerResponses[i].PreScreenerId));
}

答案 3 :(得分:0)

每次迭代,您都将使用查询返回的新值替换当前的testin。最后它可能是空的,因为找不到列表中的最后一个元素,因此返回一个空查询。之前的所有值都已被弃用。如果你想保留以前的所有值,你应该使用concat(如果你关心唯一性,则使用union)。

IEnumerable<PreScreener> testin = new List<PreScreener>();
for (int i = 0; i < respondent.PreScreenerResponses.Count; i++)
{
    testin = testin.Concat(project.PreScreeners.Where(x => x.Id == respondent.PreScreenerResponses[i].PreScreenerId));
}