在尝试捕获中使用匿名类型C#

Joe*_*der 4 c#

我为此苦苦挣扎多年,通常只是按照我的方式编写代码,但是现在是解决它的时候了。

我声明一个返回新的匿名类型的变量,并将其放在try / catch中。但是,这样做意味着它不在范围内,以后的代码显然看不到。通常我只是先声明它,然后将代码包装在try / catch中,然后像下面这样在其中重新分配:

int result = 0;

try
{
    result = 77; //real code goes here
}
catch (Exception)
{
    throw;
}
Run Code Online (Sandbox Code Playgroud)

但是这是我的真实代码,我无法弄清楚该如何做:


    try
    {
        var dt_stop = (from s in cDb.DistributionStopInformations
                       join r in cDb.DistributionRouteHeaders on s.RouteCode equals r.RouteCode
                       where r.RouteDate == s.RouteDate &&
                       r.BranchId == s.BranchId &&
                       (r.CompanyNo == companyNo && s.CompanyNo == companyNo)
                       && s.UniqueIdNo == uniqueId

                       select new
                       {
                           s,
                           r
                       }).Single();
    }
    catch (Exception)
    { //no this will not be blank
        throw;
    }
Run Code Online (Sandbox Code Playgroud)

更新:在此之后,我确实大量使用了dt_stop,如果要分配数据,我想赶上它。

我创建了以下类:

 public class StopData
{
    public DistributionStopInformation S { get; set; }

    public DistributionRouteHeader R { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用就像:

 StopData dt_stop = null;

        try
        {
            dt_stop = (from S in cDb.DistributionStopInformations
                       join R in cDb.DistributionRouteHeaders on S.RouteCode equals R.RouteCode
                       where R.RouteDate == S.RouteDate &&
                       R.BranchId == S.BranchId &&
                       (R.CompanyNo == companyNo && S.CompanyNo == companyNo)
                       && S.UniqueIdNo == uniqueId

                       select new StopData
                       {
                           S,
                           R
                       }).Single();

        }
        catch (Exception)
        {
            //YES....THERE WILL BE CODE HERE
            throw;
        }
Run Code Online (Sandbox Code Playgroud)

我收到无法使用集合初始化程序初始化类型'StopData'的信息,因为它没有实现'System.Collections.IEnumerable'

Pie*_*ier 5

匿名类型是语法糖,以避免您使用它们来命名。编译器使用匿名类型使您专注于程序要执行的操作。

出于这个原因,如果您最终引用了匿名类型,则意味着它不再是匿名的:)只要给它起一个名字,问题就会消失:

MyType dt_stop = null;
try
{
   dt_stop = (from s in cDb.DistributionStopInformations
                  join r in cDb.DistributionRouteHeaders on s.RouteCode equals r.RouteCode
                  where r.RouteDate == s.RouteDate &&
                  r.BranchId == s.BranchId &&
                  (r.CompanyNo == companyNo && s.CompanyNo == companyNo)
                  && s.UniqueIdNo == uniqueId

                  select new MyType
                  {
                      s,
                      r
                  }).Single();
}
catch (Exception)
{
   // here dt_stop can be used
   throw;
}
Run Code Online (Sandbox Code Playgroud)

MyType可以是System.Tuple标准类。要保留您的语义,可以将其设为DTO(请填写您的类型,因为我无法从您的来源推断出它们):

internal sealed class MyType
{
    public <The type of s> S {get; set;}
    public <The type of r> R {get; set;}
}
Run Code Online (Sandbox Code Playgroud)