Linq - 按ID从数据库中选择记录

时间:2022-11-27 19:55:33

I'm trying to get a record from the database with an id I specified. However I'm having difficulties creating the correct Linq query.
First off this is two of the things I've tried, but which don't seem to work.

我正在尝试从我指定的id获取数据库中的记录。但是我在创建正确的Linq查询时遇到了困难。首先,这是我尝试过的两件事,但似乎没有用。

var query_result = from d in db.Departments
                   where d.id == id
                   select d;

Here I try to select the department which follows my given id. However in my Department class the id attribute is private. Which I would like to keep that way, in compliance with normal OOP.

在这里,我尝试选择遵循我给定id的部门。但是在我的Department类中,id属性是私有的。我希望保持这种方式,符合正常的OOP。

Before that I tried doing this:

在此之前我尝试这样做:

var query_result = from d in db.Departments
                   where d.getId() == id
                   select d;

In this case I'm using the getter for the id, but that gives me the following error:

在这种情况下,我使用getter作为id,但这给了我以下错误:

Method 'Int32 getId()' has no supported translation to SQL.

方法'Int32 getId()'没有支持的SQL转换。

I've tried looking around but I can't seem to find a conclusive answer for something that should be, in my opinion, very simple. I do see a lot of examples access the class attributes directly, but that would mean making them public. Unless I'm missing something I wouldn't really want to go that way.

我试过四处寻找,但我似乎无法找到一个确凿的答案,在我看来,这应该是非常简单的。我确实看到很多示例直接访问类属性,但这意味着将它们公之于众。除非我遗漏了一些东西,否则我真的不想这样做。

I'm not very experienced in Linq (or c# for that matter). Thanks in advance.

我在Linq(或c#)方面不是很有经验。提前致谢。

2 个解决方案

#1


2  

If you're using Entity Framework, then you can't declare either the get or set as private, or else Entity Framework won't be able to access it either.

如果您正在使用Entity Framework,那么您不能将get或set声明为private,否则Entity Framework也将无法访问它。

This is what you're looking for to ensure that the outside code is unable to set the IDs:

这是您正在寻找的,以确保外部代码无法设置ID:

public int Id { get; protected internal set; }

#2


2  

Your current query is returning a list. Sounds like you need a single record? For that, use this:

您当前的查询返回一个列表。听起来你需要一个记录?为此,使用此:

var recordDesired = db.Departments.FirstOrDefault(d => d.id == id);

Assuming 'id' is an int defined somewhere, and the 'id' property is also int. Are you using entity framework by any chance? If so, I believe the 'id' property needs to have both a public getter and a public setter. I hear you on not exposing the ID to your end client application. One option would involving using DTO's (data transfer objects) to translate all the goodness that the database spews out and wrapping it in a simple class that only exposes the properties your end application cares about.

假设'id'是在某处定义的int,'id'属性也是int。您是否有机会使用实体框架?如果是这样,我相信'id'属性需要同时拥有公共getter和public setter。我听说您没有将ID暴露给最终客户端应用程序。一种选择是涉及使用DTO(数据传输对象)来翻译数据库喷出的所有优点并将其包装在一个简单的类中,该类仅公开最终应用程序关注的属性。

#1


2  

If you're using Entity Framework, then you can't declare either the get or set as private, or else Entity Framework won't be able to access it either.

如果您正在使用Entity Framework,那么您不能将get或set声明为private,否则Entity Framework也将无法访问它。

This is what you're looking for to ensure that the outside code is unable to set the IDs:

这是您正在寻找的,以确保外部代码无法设置ID:

public int Id { get; protected internal set; }

#2


2  

Your current query is returning a list. Sounds like you need a single record? For that, use this:

您当前的查询返回一个列表。听起来你需要一个记录?为此,使用此:

var recordDesired = db.Departments.FirstOrDefault(d => d.id == id);

Assuming 'id' is an int defined somewhere, and the 'id' property is also int. Are you using entity framework by any chance? If so, I believe the 'id' property needs to have both a public getter and a public setter. I hear you on not exposing the ID to your end client application. One option would involving using DTO's (data transfer objects) to translate all the goodness that the database spews out and wrapping it in a simple class that only exposes the properties your end application cares about.

假设'id'是在某处定义的int,'id'属性也是int。您是否有机会使用实体框架?如果是这样,我相信'id'属性需要同时拥有公共getter和public setter。我听说您没有将ID暴露给最终客户端应用程序。一种选择是涉及使用DTO(数据传输对象)来翻译数据库喷出的所有优点并将其包装在一个简单的类中,该类仅公开最终应用程序关注的属性。