簡體   English   中英

C#到vb.net linq選擇

[英]c# to vb.net linq select

我有這段代碼:

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

這是模型工廠的一部分:

/// <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
    };
}

當我將其放入轉換器時,由於空檢查而引發錯誤

model.Contacts?

因此,我將其刪除並按以下方式進行制作:

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

也沒有null合並運算符。 轉換者聲明該行應這樣寫:

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

但是它不會編譯。 有誰知道我怎樣才能使它工作。

PS:

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

是短的

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

Create是一個委托,在VB.NET中使用AddressOf

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

但是,您還應該在VB.NET中使用空條件運算符

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

但是VB.NET中的空合並運算符If

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM