Click here to Skip to main content
15,885,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there i'm having a problem with this wondering if anyone can help the error says:

System.InvalidOperationException: Sequence contains more than one element
Error Happens here: string active = @group.First() == item ? "active" : string.Empty;


Here my code:
C#
<ul id="myTab" class="nav nav-tabs">
    @{ var group = Model.GroupBy(g => g.ReviewID).ToList(); }
    @foreach (var item in group)                
    {
      string active = @group.First() == item ? "active" : string.Empty;

      <li class="@active"><a href="#@Model.Where(a => a.ReviewID ==      item.Key).Single().Review.PersonID" data-toggle="tab"></a></li>
     }
     <li class="form-group pull-right">
        <div class="col-md-offset-1 col-md-12">
     <input type="submit" value="Submit" class="btn btn-primary btn-default" name="Add" />
    </div>
  </li>
</ul>


And below is my action Result

C#
public ActionResult Index()
        {
            DateTime dateToday = DateTime.Now;
            DateTime firstOfThisMonth = new DateTime(dateToday.Year, dateToday.Month, 1);

            string name = User.Identity.Name;
            string newName = GetUserName(name);

            var unansweredReviews = from ur in db.Reviews
                                    where ur.PersonID == newName
                                    where ur.Responses.Any(s => s.ScoreID == null)
                                    select ur;

            if(unansweredReviews.Any())
            {
                return View(unansweredReviews.SelectMany(u => u.Responses).ToList());
            }

            var reviewForCurrentMonth = from cm in db.Reviews
                            where cm.PersonID == newName
                            where cm.DateTimeReviewed != null && cm.DateTimeReviewed <= firstOfThisMonth
                            select cm;

            if(reviewForCurrentMonth.Any())
            {
                Response.Redirect("~/Home/Index");
            }
            else
            {
                var getQuestions = db.Questions.Select(q => q.QuestionID);
                var peersToReview = db.People.Where(p => p.PersonID == newName).Single().MemberGroups.SelectMany(rg => rg.Members).ToList();

                foreach (var peers in peersToReview)
                {
                    Review reviews = new Review();
                    reviews.PersonID = newName;
                    reviews.ReviewPersonID = peers.PersonID;
                    foreach (var id in getQuestions)
                    {
                        Response responses = new Response();
                        responses.Review = reviews;
                        responses.QuestionID = id;
                        db.Responses.Add(responses);
                    }
                    db.Reviews.Add(reviews);
                } 
                db.SaveChanges();
            }
            return View();
        }
Posted
Updated 11-Aug-14 21:13pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Aug-14 3:14am    
In what line? Did you use the debugger?
—SA
Nico_Travassos 12-Aug-14 3:17am    
Line 24 : string active = @group.First() == item ? "active" : string.Empty;

I did use the debugger. Cant figure out why its happening.
Kumarbs 12-Aug-14 3:24am    
Can you make sure you are getting single value in @group.First() and item. I know group.first() gets only one value. But would like to cross check it.
Nico_Travassos 12-Aug-14 3:31am    
Just checked now i'm getting a single value.

Key: 23

1 solution

Your error seems to be generated from the next line of code:
"Model.Where(a => a.ReviewID == item.Key).Single().Review.PersonID", and especial from using of Single() over a list of results.

So you should check that code and if there are more then one result, you should replace Single() with First().
 
Share this answer
 
Comments
Nico_Travassos 12-Aug-14 4:22am    
Thank you so much :)
Raul Iloc 12-Aug-14 4:23am    
Welcome, I am glad that I could help you!
Nico_Travassos 12-Aug-14 4:22am    
Problem solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900