简体   繁体   中英

c# to vb.net linq select

I have this bit of code:

Contacts = model.Contacts?.Select(Create).ToList() ?? new List<Contact>()

which is part of a model factory:

/// <summary>
/// Creates the Account entity from the view model
/// </summary>
/// <param name="model">The Account view model</param>
/// <returns>An Account entity</returns>
public static Account Create(AccountViewModel model)
{

    // If the model is null, return null
    if (model == null) return null;

    // Return our model
    return new Account
    {
        AccountNumber = model.AccountNumber,
        AvailableDeliveryDates = model.AvailableDeliveryDates,
        BusinessCategory = model.BusinessCategory,
        Category = Enumerations.GetDescription(model.Category),
        CountryCode = model.CountryCode,
        Name = model.Name,
        Disabled = model.Disabled ? "Y" : "N",
        OnStop = model.OnStop ? "Y" : "N",
        ProductCatalog = model.ProductCatalog,
        Profile = model.Profile,
        Trade = model.Trade ? "Y" : "N",
        ShortAccountNumber = model.ShortAccountNumber,
        StatementAccountNumber = model.StatementAccountNumber,
        SalesOfficeNotes = model.SalesOfficeNotes,
        TerritoryCode = model.TerritoryCode,
        Type = Enumerations.GetDescription(model.Type),
        UnmannedAddress = model.UnmannedAddress ? "Y" : "N",

        Address = Create(model.Address),
        Contact = Create(model.PrimaryContact),
        SalesAnalysisRepresentative = model.SalesAnalysisRepresentative,
        CommissionAnalysisRepresentative = model.CommissionAnalysisRepresentative,
        ServiceRepresentative = model.ServiceRepresentative,
        Currency = new Currency
        {
            Code = model.Currency.Code,
        },
        Finance = Create(model.Finance),
        Group = new Group
        {
            Description = model.GroupName
        },                

        Contacts = model.Contacts?.Select(Create).ToList() ?? new List<Contact>()
    };
}

/// <summary>
/// Creates the Contact entity from the view model
/// </summary>
/// <param name="model">The Contact view model</param>
/// <returns>A Contact entity</returns>
public static Contact Create(ContactViewModel model)
{

    // If the model is null, return null
    if (model == null) return null;

    // Return our model
    return new Contact
    {
        Email = model.Email,
        Fax = model.Fax,
        Name = $"{ model.FirstName } { model.LastName }",
        Telephone = model.Telephone
    };
}

When I put this into a converter, it threw an error due to the null check

model.Contacts?

So I removed it and made the line as this:

Contacts = model.Contacts.Select(Create).ToList()

without the null coalescing operator too. The convert stated that the line should be written as this:

Contacts = model.Contacts.[Select](Create).ToList()

But it will not compile. Does anyone know how I can get this to work.

PS:

Contact = model.Contacts.Select(Create).ToList() 

is short for

Contact = model.Contacts.Select(m => this.Create(m)).ToList()

Create is a delegate, in VB.NET you use AddressOf

Contacts = model.Contacts.Select(AddressOf Create).ToList()

But you should also use the null-conditional operator in VB.NET:

Contacts = model.Contacts?.Select(AddressOf Create).ToList() 

But the null-coalescing operator in VB.NET is If :

Contacts = If(model.Contacts?.Select(AddressOf Create).ToList(), New List(Of Contact)())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM