Noah Crowley
Noah Crowley

Reputation: 455

"Sequence contains no matching element" on ToList()

I have a piece of code that should retrieve a List of User objects.

public List<User> GetUsersBySessions(string[] sessionStrs, string ip)
{
    if (sessionStrs == null || string.IsNullOrEmpty(ip))
        return new List<User>();
    using (var ctx = new DataContext())
    {
        var ret = ctx.Sessions.Include("User").Where(s => sessionStrs.Contains(s.ID) && s.IP.Equals(ip)).Select(s => s.User).ToList();
        return ret;
    }
}

The arguments sessionStrs and ip are properly passed into the method. However, I'm getting the following error:

error
(source: imgbomb.com)

How could this type of error be caused when I'm not using any .First() or .Single()? I'm just trying to get all items in a table that fit into an array return them as a List. I've never seen such a problem before.

The following line of code even causes an error:

var ret = ctx.Sessions.ToList();

Here is my DataContext:

public class DataContext : DbContext
{
    public GenericDataContext()
        : base(CONNECTION_STRING) //My CONNECTION_STRING is defined somewhere else, but I'm going to hide that for security's sake.
    {
    }

    public DbSet<Password> Passwords { get; set; }
    public DbSet<Session> Sessions { get; set; }
    public DbSet<User> Users { get; set; }

}

And here is my Session model:

[Table("tbl_Sessions")]
public class Session
{

    [Column("SessionID")]
    [MaxLength(24)]
    [Required]
    [Key]
    public string ID { get; set; }

    [Column("UserID")]
    [Required]
    public int UserID { get; set; }

    [Column("IP")]
    [MaxLength(24)]
    [Required]
    public string IP { get; set; }

    [ForeignKey("UserID")]
    public virtual User User { get; set; }

}

NOTE Both Classes are properly namespaced and have their proper using statements, as well.

Upvotes: 3

Views: 4583

Answers (5)

Dorin Baba
Dorin Baba

Reputation: 1728

Make sure you're using FirstOrDefault instead of First if you're extracting something from a list.

Upvotes: 0

Kevin R.
Kevin R.

Reputation: 3581

Google led me here, so I'll throw my experience out there for anyone having a similar issue.

My project was using EF fluent configuration to query an existing database. I couldn't figure out why I was receiving any data back despite being able to query the table with other tools.

After banging my head against the wall, I finally discovered that I'd mislabeled the column type like so:

Property(x => x.DateCreated)
    .HasColumnName("datetiem") // strings are the devil

Upvotes: 0

Noah Crowley
Noah Crowley

Reputation: 455

The actual answer to this question had to do with the fact that one of my models had two Keys, each of which was a string. When I changed said model to have an int as it's Key, all worked well.

Upvotes: 1

rkawano
rkawano

Reputation: 2503

Try to check if you have elements before selecting:

var ret = ctx.Sessions.Where(s => sessionStrs.Contains(s.ID) && s.IP.Equals(ip));
if (ret.Any())
    return ret.Select(s => s.User).ToList();
else
    return null; // or new List<User>();

Upvotes: 0

Tim.Tang
Tim.Tang

Reputation: 3188

May be sessionStrs is null:

public List<User> GetUsersBySessions(string[] sessionStrs, string ip)
{
    using (var ctx = new DataContext())
    {
        var ret = ctx.Sessions.Include("User");
        if(sessionStrs!=null && sessionStrs.Any())
           ret =ret.Where(s => sessionStrs.Contains(s.ID));
        if(!string.IsNullOrEmpty(ip))
           ret =ret.Where(s => s.IP.Equals(ip));

        return ret.Any()?ret.Select(s => s.User).ToList():NULL;
    }
}

Upvotes: 0

Related Questions