Linq to Sql Query两次返回相同的记录

Usm*_*lid 3 .net c# database asp.net linq-to-sql

我使用Linq to Sql查询数据库.这是我的数据:

     Name      LastName        Age
    ------------------------------
1    Abc         Def            15
2    Abc         Def            17
3    xyz         wss            17
Run Code Online (Sandbox Code Playgroud)

我的Linq to Sql代码是:

Context _context = new Context("Conn_String");
var table = _context.GetTable<Person>();
List<Person> persons = table.Where<Person>(p => p.Name == "Abc" && p.LastName == "Def").ToList<Person>();
Run Code Online (Sandbox Code Playgroud)

据我了解,此查询应返回2条记录.即记录1和记录2.但它返回记录1两次.如果它是Linq to Sql中的错误或我做错了什么,你能告诉我吗?

编辑:

这是我的DAL代码:

public List<T> GetList<T>(Expression<Func<T, bool>> predicate) where T : class
{
     try
     {
          Context _context = new Context("Conn_String");
          var table = _context.GetTable<T>();
          return table.Where<T>(predicate).ToList<T>();
      }
      catch (Exception ex)
      {
           throw ex;
      }
 }
Run Code Online (Sandbox Code Playgroud)

我将此方法称为:

List<Person> person = DAL.GetList<Person>(p => p.Name == "Abc" && p.LastName == "Def");

foreach(var Person in persons )
{
    // Print(person.Age);
}
Run Code Online (Sandbox Code Playgroud)

小智 7

我自己也遇到过这个问题.检查模型推断的哪个属性是Person类的实体键.

如果它具有非唯一列作为实体键,则它将使用与使用ToList()进行转换时每行的值匹配的第一行