Linq to SQL specflow,无法隐式转换类型 'System.Collections.Generic.List<'

Linq to SQL specflow, Cannot implicitly convert type 'System.Collections.Generic.List<'

我在规范流中使用 linq to sql 查询,我需要将查询结果传递给绑定 Class 中的私有字段,以便我可以在稍后的步骤文件,但我尝试的一切都会遇到错误

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type' to 'System.Collections.Generic.List<string>'

最终查询将包含 15 - 20 个变量。关于如何在绑定中定义我的私有字段的任何想法 Class?

public class Member_Dim_OrganizationsSteps
{

    connectionDataContext cMain = new connectionDataContext();

    static private List<string> mainString;

    [Given(@"I have selected the MemberOrganizations field values from MemberOrganizations")]
    public void GivenIHaveSelectedTheMemberOrganizationsFieldValuesFromMemberOrganizations()
    {

        var mainResults = (from mo in oecMain.MemberOrganizations
                              orderby mo.OrgID
                              select new
                              {
                                  OrgID = (mo.OrgID),
                                  OrgKey = (mo.OrgKey != null ? mo.OrgKey : "").Trim().ToUpper(),
                                  BaseOrgKey = (mo.OrgKey != null ? mo.OrgKey.Substring(0, 11) : "-----").ToUpper(),
                                  ManufacturerID = (Convert.ToInt16(mo.ManufacturerID != null ? mo.ManufacturerID : 0)),
                              });


        mainString = mainResults.ToList();


    }

如果要保留查询结果,则需要定义具体类型而不是使用匿名类型:

public class Result
{
    public int OrgID {get; set;}
    public string OrgKey {get; set;}
    public string BaseOrgKey {get; set;}
    public int ManufacturerID {get; set;}
}

使用该类型作为您的列表类型

private List<Result> mainString;

然后在您的查询中投影到该类型:

var mainResults = (from mo in oecMain.MemberOrganizations
                   orderby mo.OrgID
                   select new Result
                   {
                     OrgID = (mo.OrgID),
                     OrgKey = (mo.OrgKey != null ? mo.OrgKey : "").Trim().ToUpper(),
                     BaseOrgKey = (mo.OrgKey != null ? mo.OrgKey.Substring(0, 11) : "-----").ToUpper(),
                     ManufacturerID = (Convert.ToInt16(mo.ManufacturerID != null ? mo.ManufacturerID : 0)),
                   });