Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC中组合两个对象的C Web API DTO_C#_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

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

C# 在MVC中组合两个对象的C Web API DTO,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,嗨,我是一个C和DTO的新手,我正在寻找一些关于编写方法的建议。基本上我有两个传输对象,成员和源。我试图实现的是显示来自特定来源的成员列表 唯一的问题是我需要能够显示与SourceRef中的SourceId关联的成员。因为我不想传递敏感的MemberID和SourceId,所以每个都有一个引用id,这就是我在API中识别它们的方式 成员对象 public class MemberObj { public int memId { get; set; } public String

嗨,我是一个C和DTO的新手,我正在寻找一些关于编写方法的建议。基本上我有两个传输对象,成员和源。我试图实现的是显示来自特定来源的成员列表

唯一的问题是我需要能够显示与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);
但这不会返回任何内容。

以下内容接受代码作为sourceRef,并返回ref对应的SourceID

从这里开始,它只需将所有成员筛选为具有匹配sourceID的成员。我附近没有VS的副本,所以语法可能不正确!如果记事本++有智能的话

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);
}
这应该起作用:

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

    if(thisSrc == null)
       return Enumerable.Empty<MemberObj>();

    return db.Members.Where(m => m.sourceId == thisSrc.sourceId);
}
请注意,当给定代码有多个源时,您应该处理这种情况,SingleOrDefault在这种情况下抛出异常。
如果您确定不是这种情况,请使用FirstOrDefault。

在哈姆雷特的答案中,您可以这样做,以返回DTO而不是您的成员实体:

public IEnumerable<MemberDTO> GetMem(String code)
{
    //Get the source object by sourceRef
    var thisSrc = db.Sources
                    .Where(s => s.sourceRef == code)
                    .SingleOrDefault();

    if(thisSrc == null)
       return Enumerable.Empty<MemberObj>();

   //Filter Members by sourceId and map the results to a MemberDTO with less fields
   var data = (from m in db.Members
               where m.sourceId == thisSrc.sourceId
               select new MemberDTO
               {
                   MemberId = m.memId,
                   //map the rest of your DTO fields..

               }).ToList();

    return data;
}

请给我们更多您想要的。我的问题是,我似乎无法编写一个基于SourceRef返回成员的工作方法。什么不工作?你有什么具体的错误吗?更多的是逻辑/语法错误,我不知道如何让这两个对象一起工作。由于我的成员DTO不包含sourceRef,但如果where子句不包含,我希望能够按sourceRef显示成员。Whereb=>b.sourceId==thisSrc.sourceId?这会不会返回数据库对象而不是我的DTO??只有my db.Members有大量字段,而my DTO只过滤到5个字段。@Freeman为此,请创建匿名类型的实例,然后将它们映射到您的DTO。这会不会引发问题,因为代码和sourceRef是字符串,而新的sourceID变量将是整数。这会导致确认吗?除非我缺少某些内容,我不相信会这样,因为您将字符串分别与ID进行比较,所以它们永远不会冲突。这不是db.Sources.Wheres=>s.sourceRef==代码返回例如RFC12,但将其存储到int中吗?啊,是的,我知道了,我甚至不知道如何将其过滤为特定类型,如.sourceID,谢谢!