linq查询结果中缺少属性

本文关键字:属性 查询 结果 linq | 更新日期: 2023-09-27 18:22:48

请在下面找到linq查询

var result = (
    from sd in students
    select sd.Student_ID
  ).Except(
    from m in query1 
    select m.Student_ID
  ).ToList();

从这个查询中,我得到了确切的结果。现在我想填充学生的其他数据,所以我所做的是编写其他linq查询。下面是,但我没有得到r.Student_ID属性来与学生表进行比较Intellisense未提供r.Student_ID。请帮助!

var finalResult = (
    from sd in dbcDefaulter.Student_Details 
    from r in result 
    where r == sd.Student_ID
    orderby sd.Student_ID
    select new { sd.Student_ID, sd.Name, sd.Class, sd.Section, sd.F_Name, sd.F_Mobile }
  ).Distinct().ToList();

请在此处找到完整的designer.cs

Student_Details类的一部分如下所示:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Student_Detail")]
public partial class Student_Detail
{
    private int _Student_ID;
    private string _Class;
    private string _Section;
    private string _Session;
    private string _Name;
    private System.Nullable<System.DateTime> _B_Date;
    private string _E_Mail;
    private string _Gender;
    private string _F_Name;

    //From Jesse: Please insert the property definition for Student and/or Students, and/or Student_ID here if available.
}

get set属性的一部分如下所示:

public DefaulterDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
                base(connection, mappingSource)
        {
            OnCreated();
        }
        public System.Data.Linq.Table<Student_Detail> Student_Details
        {
            get
            {
                return this.GetTable<Student_Detail>();
            }
        }
        public System.Data.Linq.Table<Fees_Due> Fees_Dues
        {
            get
            {
                return this.GetTable<Fees_Due>();
            }
        }

请找到我们在第一个查询result中使用的query1students的定义。所以query1是-

var query1 = (from fd in dbcDefaulter.Fees_Dues
                          join sd in dbcDefaulter.Student_Details on Convert.ToInt32(fd.Student_ID) equals sd.Student_ID
                          where fd.Session == GlobalVariables.Session && (fd.Month.Contains(cmbMonth.SelectedItem.ToString()))
                          orderby fd.Student_ID
                          select new { sd.Student_ID, fd.Month, fd.Session }).Distinct();

CCD_ 7为-

var students = (from sd in dbcDefaulter.Student_Details
                        select sd).ToList();

linq查询结果中缺少属性

您可以考虑使用List<T>.Contains(aValue)而不是加入;这也解决了您在提供的另一个解决方案中使用Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Linq.IQueryable<int>'时遇到的问题:

var result = (
    from sd in students
    select sd.Student_ID
  ).Except(
    from m in query1 
    select m.Student_ID
  ).ToList();
var finalResult = (
    from sd in dbcDefaulter.Student_Details 
    where result.Contains(sd.Student_ID)
    orderby sd.Student_ID
    select 
       new { sd.Student_ID, sd.Name, sd.Class, sd.Section, sd.F_Name, sd.F_Mobile}
).Distinct().ToList();

根据您提供的信息,这就是您应该如何更新代码:

 // Updated first query to return the Student_ID in an anonymous class.
 var result = (from sd in students select new {Student_ID = sd.Student_ID})
     .Except(from m in query1 select m.Student_ID).ToList();
 // Updated second query to use Join statement
 var finalResult = (
     from sd in dbcDefaulter.Student_Details
     join r in result on sd.Student_ID equals r.Student_ID
     orderby sd.Student_ID
     select new {sd.Student_ID, sd.Name, sd.Class, sd.Section, sd.F_Name, sd.F_Mobilez
 ).Distinct().ToList();