gpt4 book ai didi

C# Web API DTO 在 MVC 中结合两个对象

转载 作者:行者123 更新时间:2023-11-30 16:50:38 26 4
gpt4 key购买 nike

大家好,我是 C# 和 DTO 的新手,我正在寻找有关编写方法的一些建议。基本上我有两个传输对象,Members 和 Source。我想要实现的是显示来自特定来源的成员列表。

唯一的问题是我需要能够显示与 SourceRef 中的 SourceId 关联的成员。因为我不想传递敏感的 MemberID 和 SourceId,所以每个都有一个引用 ID,这就是我将如何在我的 API 中识别它们

成员对象

public class MemberObj
{
public int memId { get; set; }
public String memRef { get; set; }
public String fName { get; set; }
public String lName { get; set; }
public String email { get; set; }
public String detail { get; set; }
public int sourceId { get; set; }
}

源对象

public class SourceObj
{
public int sourceId { get; set; }
public String sourceRef { get; set; }
}

所以我想去地址举例

http://..../api/Member/Source/{sourceRef}

并显示通过 sourceRef 关联到 sourceId 的成员列表

我想出了一些类似的思路......

public IEnumerable<MemberObj> GetMem(String code)
{
var sc = db.Sources;
var thisSrc = sc.Where(s => s.sourceRef == code).SingleOrDefault();


return db.Members.Select(s => new MemberObj
{
memId = s.memId,
firstName = s.firstName,
lastName = s.lastName,
email = s.emailAddress,
memRef = s.memRef

}).AsEnumerable().Where(s => s.sourceRef== thisSrc.sourceRef);

但这没有任何返回。

最佳答案

以下接受 code 作为 sourceRef 并返回 ref 对应的 SourceID

从这里开始,它只是将所有成员过滤为仅具有匹配 sourceID 的成员。 (我附近没有 VS 的副本,所以语法可能已经过时了!要是 Notepad++ 有智能感知就好了……)

public IEnumerable<MemberObj> GetMem(String code)
{
int soureID = db.Sources.Where(s => s.sourceRef == code).SingleOrDefault().sourceID; //I'm assuming code is the source ref??

//Insert and handle your sourceID == 0 checks here.
//...

return db.Members.Where(m => m.sourceId == sourceID);
}

关于C# Web API DTO 在 MVC 中结合两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34765767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com