2014-09-20 92 views
1

缺少请找到该查询我得到确切的结果LINQ查询属性从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属性与学生表进行比较。智能感知不给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(); 

请找到Student_Details类的完整designer.cshere

部分看起来是这样的:

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

请找0123的定义和students,我们已经在第一个查询中使用了result。所以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(); 
+0

对不起;(,我是新手,我的LINQ不明白你是什么意思,请你给我一个样品/例如? – 2014-09-20 10:21:49

+0

请改为将其编辑到您的问题 – 2014-09-20 14:09:08

+0

什么是列表中的''列表'?'是什么类型的? – 2014-09-20 14:09:49

回答

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

我收到错误消息,sd不包含学生的定义。在'sd.Student.Student_ID'上得到错误 – 2014-09-20 13:43:46

+0

我在上面分享了定义类 – 2014-09-20 13:57:45

+0

我已经在问题 – 2014-09-20 16:37:11

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

无法给出减少答案的原因称为嫉妒。它会导致你无处.. .. – codebased 2014-09-20 10:29:07

+0

帮我一个忙@jessehouwing开始阅读其他人说什么,而你知道什么。你甚至没有看到我的答案。你可能是主人,但不久后你会走过同样的道路。无论如何,祝你好运。 – codebased 2014-09-20 13:37:45

+0

嗨Codebased,这是你灵魂的反应。您提供的解决方案无法正常工作。 **它说r不包含Student_ID **的定义。你可以帮我吗? – 2014-09-20 13:39:00