LINQ返回类型并在其他函数

时间:2017-01-11 00:53:19

标签: c# linq

我试图从LINQ查询中获取结果并将其传递给不同的函数以在那里使用。

这是我从浏览器调用的第一个函数。然后,调用GetAddressByIncID

 // GET: Hello/5
public void GetModel(int? incID)
    {
        var incAddress = GetAddressByIncID(incID);
        var siteID = GetSiteIDbyAddress(incAddress);
        LoadSite(siteID);
    }

private ?returntype? GetAddressByIncID(int? incID)
    {
        var incAddress = (from address in db.Incs
                    where address.IncID == incID
                    select new
                    {
                        IncID = address.IncID,
                        Number = address.Number,
                        Direction = address.Direction,
                        StreetName = address.StreetName,
                        Zip = address.Zip,
                        City = city.CityDesc,
                        State = state.StateDesc
                    }).FirstOrDefault();
        return incAddress;
    }

此时,我可以获得我想要的查询结果。我只需要一个或null,所以我设置FirstOrDefault()。 在这个函数中,我能够访问incAddress内部的值,比如incAddress.IncID,我希望在传递给它们时在其他函数中执行此操作。因为它的类型是匿名类型,我不确定应该使用什么返回类型。

一旦返回,我将其传递给调用GetSiteIDbyAddress的其他函数以找出SiteID。这个结果可能是多次。

    private ?returntype? GetSiteIDbyAddress(string incAddress)
        {    
          var searchaddress = (from address in db.Addresses
                                     where PrimaryAddressNumber == incAddress.Number && Direction == incAddress.Direction && StreetName == incAddress.StreetName
                                     select new
                                     {
                                         SiteID = address.SiteID
                                     });

            return searchaddress;
        }

请您就第一和第二功能的退货类型给我一个建议吗?我搜索并尝试过(列表,数组等),但找不到一个很好的解决方案。

修改

根据建议,我编辑了我的代码,如下所示。现在我可以使用IncAddress中的值传递给另一个函数。但是,出现了另一个问题。

public class IncAddress
    {
        public int IncID { get; set; }
        public string Number { get; set; }
        .
        .
    }
public class IncSite
{
    public int? SiteID { get; set; }
    .
    .
}

private IncAddress GetAddressByIncID(int incID)
    {
        var incAddress = (from address in db.Incs
                    where address.IncID == incID
                    select new IncAddress
                    {
                        IncID = address.IncID,
                        Number = address.Number,
                        Direction = address.Direction,
                        StreetName = address.StreetName,
                        Zip = address.Zip,
                        City = city.CityDesc,
                        State = state.StateDesc
                    }).FirstOrDefault();
        return incAddress;
    }

private IncSite GetSiteIDbyAddress(IncidentAddress incidentAddress)
    {    
      var searchaddress = (from address in db.Addresses
                                 where PrimaryAddressNumber == incAddress.Number
                                 select new IncSite
                                 {
                                     SiteID = address.SiteID
                                 });
        return searchaddress;
    }

GetSiteIDbyAddress上,我收到错误消息:

  

无法隐式转换类型'System.Linq.IQueryable<   HelloWorld.Controllers.HelloController.IncSite> ' 至   'HelloWorld.Controllers.HelloController.IncSite'。一个明确的   存在转换(你错过了演员吗?)

我认为这是因为我没有放FirstOrDefault(),因为我希望此查询可以找到多条记录。如何完成此功能以获得多重结果?

1 个答案:

答案 0 :(得分:1)

您可以通过两种方式执行此操作, 通过创建新类让它为Address,将所选值作为字段,在这种情况下,返回类型将尊重{{ 1}}(如果使用FirstOrDefault则为Address;如果使用Address则为List,依此类推)。 您可以将返回类型指定为.ToList(),在这种情况下,不需要进行其他更改。但我建议你尽可能创建一个类,并避免在这种情况下使用dynamic,因为

  

速度慢很多,并且没有相同的编译时错误检查   作为一个适当的班级

根据David的评论进行更新

案例1:

dynamic

方法应该是这样的:

//definition should be something like this
public class Address
{
    public string IncID { get; set; }
    public string Number { get; set; }
    public string Direction { get; set; }
    public string StreetName { get; set; }
    public string Zip { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

案例2

private Address GetAddressByIncID(int? incID)
{
    var incAddress = (from address in db.Incs
                where address.IncID == incID
                select new Address
                {
                    IncID = address.IncID,
                    Number = address.Number,
                    Direction = address.Direction,
                    StreetName = address.StreetName,
                    Zip = address.Zip,
                    City = city.CityDesc,
                    State = state.StateDesc
                }).FirstOrDefault();
    return incAddress;
}