Base solution for your next web application
Open Closed

Convert type 'List to 'Abp.NameValue<string>' #4623


User avatar
0
ramezani583 created

Hi, How to convert type 'System.Collections.Generic.List<Province>' to 'Abp.NameValue<string>'

public List<NameValue<string>> GetProvincesForCombo(string searchTerm)
        {

            var provinces = _provinceRepository
                .GetAll()
                .WhereIf(
                    !searchTerm.IsNullOrWhiteSpace(),
                    p =>
                        p.Name.ToLower().Contains(searchTerm.ToLower())
                )
                .OrderBy(p => p.Name);
             
            
            return (NameValue<string>) provinces;
        }

2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team
    • Your value is a Province, not a string.
    • You cannot cast an object to a type unless it actually is that type or a subclass.
    return provinces.Select(p => new NameValue<Province>(p.Name, p))
                    .ToList();
    
  • User Avatar
    0
    AstarIT created

    INTERFACE List<NameValue<string>> GetAllBusinessType(string searchTerm);

    SERVICE private readonly IRepository<BusinessType> _businesstypeRepository;

    	[AbpAuthorize(AppPermissions.Pages_Administration_TenantExtendeds)]
    	public List&lt;NameValue&lt;string&gt;> GetAllBusinessType(string searchTerm)
    	{
    
    		var busineestype = _businesstypeRepository.GetAll().WhereIf(
    				!searchTerm.IsNullOrWhiteSpace(),
    				p => p.Name.ToLower().Contains(searchTerm.ToLower())
    			)
    			.OrderBy(p => p.Name);
    
    		
    
    		return busineestype.Select(p => new NameValue&lt;BusinessType&gt;(p.Name, p)).ToList();
    
    
    	}
    

    PAGE <div class="form-group"> <label for="TenantExtended_BusinessType">@L("Business Type")</label> @Html.DropDownList("businesstype", new SelectList(Model.TenantExtendedBusinessTypeList, "Id", "DisplayName", (Model.IsEditMode ? Model.TenantExtended.BusinessType.ToString() : "")), @L("Select Business Type"), new { @class = "form-control kt-select2" }) </div>

    JQUERY $(".kt-select2").select2({ placeholder: 'Select', ajax: { url: abp.appPath + "api/services/app/TenantExtendeds/GetAllBusinessTypeForTableDropdown", dataType: 'json', delay: 250, data: function (params) { return { searchTerm: params.term, // search term page: params.page }; }, processResults: function (data, params) { params.page = params.page || 1;

                return {
                    results: $.map(data.result, function (item) {
                        return {
                            text: item.name,
                            id: item.value
                        }
                    }),
                    pagination: {
                        more: (params.page * 30) &lt; data.result.length
                    }
                };
            },
            cache: true
        },
        minimumInputLength: 1,
        language: abp.localization.currentCulture.name
    });
    

    ========= On the below line on SERVICE got the error, and I am following the same advise I .

    return busineestype.Select(p => new NameValue<BusinessType>(p.Name, p)).ToList();

    Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<Abp.NameValue<OneServDemo.BusinessTypes.BusinessType>>' to 'System.Collections.Generic.List<Abp.NameValue<string>>' OneServDemo.Application E:\Multi Tenant Demos\OneNetZero Demos\OneServ DropDownBox\cesarzam26\OneServDemo\src\OneServDemo.Application\TenantExtendeds\TenantExtendedsAppService.cs 257 Active

    Can you please help me out on this I have work all day on this and I can get it resolved. All help it is really appreciated. Thank you