首页 \ 问答 \ 实体框架加入(Entity Framework Join)

实体框架加入(Entity Framework Join)

我尝试加入实体框架,但我需要在加入我的常规方法后列出我的信息,如下所示:

public DataTable LoadAllAllowing()
{
    var resultList = new DataTable();
    SqlConnection connection = new SqlConnection(@"server=. ; database=PersonnelTrackingSystem;Trusted_Connection=yes");
    connection.Open();
    SqlDataAdapter da = new SqlDataAdapter(@"SELECT PA.personelId
                                                 ,[allowedDatesId]
                                                ,[allowedStartDate]
                                                 ,[allowedEndDate]
                                                 ,[allowReason]
                                                ,PI.personnelNumber
                                                ,PI.personnelName
                                                ,PI.personnelSurname
                                                FROM [PersonnelTrackingSystem].[dbo].[personnelAllowedDates] AS PA
                                                JOIN PersonnelInformation AS PI ON PI.personelId=PA.personelId", connection);
    da.Fill(resultList);
    return resultList;
}

我如何用实体框架做到这一点? 我确实喜欢这个,但它不起作用

  public List<PersonnelInfoAndAllow> LoadAllAllowing()
    {
        PersonnelTrackingSystemEntities entity = new PersonnelTrackingSystemEntities();
        //var personnel = entity.personnelAllowedDates.ToList();
        var allows = (from allow in entity.personnelAllowedDates join personnel in entity.PersonnelInformation on allow.personelId equals personnel.personelId select new { personelId = personnel.personelId, personnelNumber = personnel.personnelNumber, personnelName = personnel.personnelName, personnelSurname = personnel.personnelSurname, allowedDatesId = allow.allowedDatesId, allowedStartDate = allow.allowedStartDate, allowedEndDate = allow.allowedEndDate, allowReason = allow.allowReason }).ToList();
        List<PersonnelInfoAndAllow> personnels = allows;
        return personnels;}

我写这样的课:

namespace personnelTrackingSystem { class PersonnelInfoAndAllow { public int personelId { get; set; } public System.DateTime allowedStartDate { get; set; } public System.DateTime allowedEndDate { get; set; } public string allowReason { get; set; } public int allowedDatesId { get; set; } public string personnelNumber { get; set; } public string personnelName { get; set; } public string personnelSurname { get; set; } } } 但它不起作用。 我怎么解决这个问题?


I try to do join with entity framework but I need to List my information after join my normal method like this:

public DataTable LoadAllAllowing()
{
    var resultList = new DataTable();
    SqlConnection connection = new SqlConnection(@"server=. ; database=PersonnelTrackingSystem;Trusted_Connection=yes");
    connection.Open();
    SqlDataAdapter da = new SqlDataAdapter(@"SELECT PA.personelId
                                                 ,[allowedDatesId]
                                                ,[allowedStartDate]
                                                 ,[allowedEndDate]
                                                 ,[allowReason]
                                                ,PI.personnelNumber
                                                ,PI.personnelName
                                                ,PI.personnelSurname
                                                FROM [PersonnelTrackingSystem].[dbo].[personnelAllowedDates] AS PA
                                                JOIN PersonnelInformation AS PI ON PI.personelId=PA.personelId", connection);
    da.Fill(resultList);
    return resultList;
}

How I can do this with entity framework? I did like this but it doesn't work

  public List<PersonnelInfoAndAllow> LoadAllAllowing()
    {
        PersonnelTrackingSystemEntities entity = new PersonnelTrackingSystemEntities();
        //var personnel = entity.personnelAllowedDates.ToList();
        var allows = (from allow in entity.personnelAllowedDates join personnel in entity.PersonnelInformation on allow.personelId equals personnel.personelId select new { personelId = personnel.personelId, personnelNumber = personnel.personnelNumber, personnelName = personnel.personnelName, personnelSurname = personnel.personnelSurname, allowedDatesId = allow.allowedDatesId, allowedStartDate = allow.allowedStartDate, allowedEndDate = allow.allowedEndDate, allowReason = allow.allowReason }).ToList();
        List<PersonnelInfoAndAllow> personnels = allows;
        return personnels;}

I write class like that :

namespace personnelTrackingSystem { class PersonnelInfoAndAllow { public int personelId { get; set; } public System.DateTime allowedStartDate { get; set; } public System.DateTime allowedEndDate { get; set; } public string allowReason { get; set; } public int allowedDatesId { get; set; } public string personnelNumber { get; set; } public string personnelName { get; set; } public string personnelSurname { get; set; } } } But it doesn't work. How I fix this ?


原文:https://stackoverflow.com/questions/32907194
更新时间:2023-04-09 07:04

最满意答案

相关问答

更多
  • 基本上Spring是一个依赖注入的框架,它是一种允许构建非常分离的系统的模式。 问题 例如,假设您需要列出系统的用户,然后声明一个名为UserLister的接口: public interface UserLister { List getUsers(); } 也许实现访问数据库以获取所有用户: public class UserListerDB implements UserLister { public List getUsers() { // ...
  • 有不同设计模式的负载,但有一些明显的设计模式: 代理 - 在AOP中大量使用和远程处理 。 在Spring配置文件中定义的Singleton - bean默认是单例。 模板方法 - 广泛用于处理样板重复代码(如关闭连接等)。 例如JdbcTemplate , JmsTemplate , JpaTemplate 。 更新以下注释:对于MVC,您可能需要阅读MVC参考 MVC中使用的一些明显的模式: 模型视图控制器 :-)。 Spring MVC的优点是您的控制器是POJO,而不是servlet。 这使得控制器 ...
  • 我已经在https://stackoverflow.com/questions/1549472/what-are-upcoming-trends-in-software-industry-and-its-impact-for-java-developer/1549740#1549740中给出了部分答案,但我在这个答案中添加一些链接。 其实我不会介绍或讨论Spring的技术质量,因为它们不是新的,不能在我看来解释嗡嗡声。 相反,请考虑以下事件和收购: 2008年11月11日: SpringSource收购G2 ...
  • 官方的快速入门指南提供了一步一步的指南,让您可以使用Stripes进行启动和运行(该指南涵盖了基本配置以及第一个Stripes应用程序的开发)。 如果您不理解说明,那么我建议遵循@ seanizer的建议,并首先从Web层上的Java EE教程开始。 在进一步学习之前,你确实需要掌握基本技能。 不要把马车放在马前。 The official Quick Start Guide provides a step by step guide to get you up and running with Strip ...
  • 对于初学者这些网站是优选的: 教程点 玫瑰印度 java2s dzone 你可以在youtube上获得hundrades的视频。 教程点是新手的最佳选择。 春天我们有很多电子书,只是去谷歌搜索。 选一本你的标准可以理解的书。 祝一切顺利.. for beginners these sites are preferable: tutorials point rose india java2s dzone you can get hundrades of videos in youtube. tutorials ...
  • 该代码未使用| 在if语句中。 它在if语句的主体中使用它是非常不同的。 在那里使用时,它是按位或操作。 编辑: @RohitJain在他的评论中提供了比我更好的链接: http : //en.wikipedia.org/wiki/Bitwise_operation#OR 如果它在if语句中使用,如下所示: if (foo() | bar()) 这意味着“不要短路”。 如果foo()返回“true”,它仍将评估bar() 。 如果你用过|| 并且foo()返回“true”,它不会计算bar()因为它知道i ...
  • 看看这个javascript逻辑运算符列表: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Logical_Operators 和比较运算符: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Comparison_Operators Check out this list of javascript logical ...
  • 你可以简单地使用NSView的setBoundsRotation:方法来设置它而不需要CoreAnimation。 如果你真的想使用CoreAnimation,你会这样: // make NSView myView a layer-backed view [myView setWantsLayer:YES]; // now get that CALayer and set the affineTransform of it, specifying the angle [myView.layer setAff ...
  • 您可以通过以下链接了解更多信息: 春季论坛 弹簧3-0-VS-Java的EE-6-0 Spring vs Java EE 为什么Java EE失去了和春天赢了 You can go through below links to know more : spring forum spring-3-0-vs-java-ee-6-0 Spring vs Java EE Why Java EE Lost and Spring Won
  • 如果您想使用Spring Integration的TCP和UDP支持 ,请求您只是接收UDP消息并对该消息执行某些操作,您应该按照以下步骤操作: 创建消息使用者 package com.example.udp; import org.springframework.messaging.Message; public class UDPConsumer { @Autowire what you want, this will be a Spring Bean @ServiceActiv ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。