Sandeep Kushwah
Sandeep Kushwah

Reputation: 592

Attribute Missing from linq query result

Please find below the linq query

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

from this query I am getting the exact results. Now I want to populate other data of students so what I have done is wrote other linq query. Below here, But I am not getting r.Student_ID attribute to compare with students table. Intellisense not giving r.Student_ID. Please help!

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

Please find Complete designer.cs here

Part of the Student_Details class looks like this:

[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.
}

Part of the get set properties looks like this:

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

Please find the definition of query1 and students that we have used in very first query that is result. So query1 is -

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

and students is -

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

Upvotes: 1

Views: 338

Answers (2)

jessehouwing
jessehouwing

Reputation: 115037

Instead of joining, you could consider using List<T>.Contains(aValue); this also solves the issue you're getting with the Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Linq.IQueryable<int>' in the other solution provided:

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

Upvotes: 2

codebased
codebased

Reputation: 7073

Based on the information that you have provided this is how you should update the code:

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

Upvotes: 0

Related Questions