【问题标题】:Attribute Missing from linq query resultlinq 查询结果中缺少属性
【发布时间】:2014-09-20 10:16:16
【问题描述】:

请在 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.cshere

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

请找到我们在第一个查询中使用的query1students 的定义,即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();

【问题讨论】:

  • 对不起 ;(,我是 linq 的新手。我不明白你的意思,你能给我一个示例/例子吗?
  • 请将其编辑到您的问题中。
  • listfrom r in list 一样是什么?它的类型是什么?
  • 你的意思是r的类型吗?我希望result 查询返回满足条件的student_ID 列表。这样我就可以使用Student_ID 的列表来过滤更多结果,就像我在问题中的finalResult 查询中使用的那样。但我没有收到r.Student_ID :( :(

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


【解决方案1】:

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

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

【讨论】:

  • 我收到错误,即 sd 不包含 Student 的定义。在sd.Student.Student_ID 出现错误
  • 我分享了上面的定义类
  • 我在问题中分享了班级的get set property
  • 我想附上我的完整代码文件,以便您浏览,您能告诉我怎么做吗?在哪里做?
  • 将它们放在 pastebin.com 上并发布链接。
【解决方案2】:

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

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

【讨论】:

  • 不给出减少答案的理由被称为嫉妒。它会让你无处可去....
  • 帮我一个忙@jessehouwing 开始阅读其他人要说的内容,而不是您所知道的内容。你甚至没有看到我的回答。你可能是大师,但迟早你会走上同样的道路。无论如何,祝你好运。
  • 您好 Codebased,这是对您的灵魂的回应。您提供的解决方案不起作用。 它表示 r 不包含 Student_ID 的定义。你能帮帮我吗?
  • 你确定你写的第一个查询是这样的: var result = (from sd in students select new {StudentID = sd.Student_ID}).Except(from m in query1 select m.Student_ID) .ToList();
  • 抱歉忽略了解决方案 :(。我已经更正了我的 result 查询。之后我收到编译错误 Expected contextual keyword 'equals' 和红色下划线orderby finalresult 查询中的关键字
猜你喜欢
  • 2019-02-14
  • 2014-05-12
  • 2019-05-27
  • 1970-01-01
  • 2015-05-12
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
相关资源
最近更新 更多