属性缺少linq查询结果

时间:2014-09-20 10:16:17

标签: c# sql linq linq-to-sql

请在下面找到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 here

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>();
            }
        }

请查看我们在query1的第一个查询中使用的studentsresult的定义。所以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();

students是 -

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

2 个答案:

答案 0 :(得分:2)

您可以考虑使用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();

答案 1 :(得分:0)

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

 // 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();