简体   繁体   中英

C# cannot convert object type array to string type array while using AutocompleteStringCollection

I want to use texbox autocomplete using database. The database query returns a list of names using join statement from three tables. Now I got an erorr when using .ToArray():

Error: "Can not convert Source type Purchase[] to target type string[]"

Here is my code:

PurchaseManager _aPurchaseManager = new PurchaseManager();

string[] productNameFromJoinedDatabase = _aPurchaseManager.GetProductNameWithCategoryAndBrandName();

productNameTextBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
productNameTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource;


var autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(productNameFromJoinedDatabase);
productNameTextBox.AutoCompleteCustomSource = autoComplete;

Update:

After using:

       string[] productNameFromJoinedDatabase = _aPurchaseManager.GetProductNameWithCategoryAndBrandName().Select(x => x.ProductName).ToArray();

The previous error has gone. But it's not working for autocomplete!

Here is my Purchase Class:

class Purchase
{
    public Int64 PurchaseId { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }
    public double TotalPurchasePrice { get; set; }
    public DateTime DateTime { get; set; }

    public CategoryEntry CategoryEntry { get; set; }
    public BrandEntry BrandEntry { get; set; }
    public Product Product { get; set; }
    public Purchase()
    {
        CategoryEntry = new CategoryEntry();
        BrandEntry = new BrandEntry();
        Product = new Product();
    }
}

Here is my database class:

public List<Purchase> GetProductNameWithCategoryAndBrandName()
    {

        List<Purchase> _aPurchaseList = new List<Purchase>();

        _connection.Open();
        string query = string.Format("SELECT CategoryEntryTable.Name, BrandEntryTable.Name, ProductNameExtentionEntryTable.ProductNameExtention " +
                                     "FROM ProductNameExtentionEntryTable " +
                                     "JOIN CategoryEntryTable " +
                                     "ON ProductNameExtentionEntryTable.CategoryId = CategoryEntryTable.CategoryId " +
                                     "JOIN BrandEntryTable " +
                                     "ON ProductNameExtentionEntryTable.BrandId = BrandEntryTable.BrandId");
        _command = new SqlCommand(query, _connection);
        SqlDataReader aReader = _command.ExecuteReader();

        if (aReader.HasRows)
        {
            while (aReader.Read())
            {
                _aPurchase = new Purchase();
                _aPurchase.CategoryEntry.Name = (aReader[0]).ToString();
                _aPurchase.BrandEntry.Name = (string)aReader[1];
                _aPurchase.Product.ProductNameExtention = (string)aReader[2];

                _aPurchaseList.Add(_aPurchase);
            }
        }
        _connection.Close();
        return _aPurchaseList;
    }

Try _aPurchaseManager.GetProductNameWithCategoryAndBrandName().Select(x => x.ProductName).ToArray(); or whatever is the right product name property.

The problem here (I think) is that GetProductNameWithCategoryAndBrandName is returning an array of Purchase instead of names, so the extra Select is to get just the names.

If you think it should be returning names instead, then try fixing the method to return just the names.

Update: To get a combined name, you can use something like this:

_aPurchaseManager.GetProductNameWithCategoryAndBrandName().Select(x => string.Format("{0} {1} {2}", x.CategoryEntry.Name, x.BrandEntry.Name, x.Product.ProductNameExtention)).ToArray();

Not that the string.Format is joining them (delimited by space but you can use any character to delimit) into one string.

Looks like GetProductNameWithCategoryAndBrandName() returns an array of Purchase objects, and it's telling you that you can't simply stuff that into a string array.

string[] productNameFromJoinedDatabase
    = _aPurchaseManager.GetProductNameWithCategoryAndBrandName();

You'll have to do the conversion yourself, most likely selecting the property you need for autocompletion.

Perhaps something like this (untested), assuming the "Name" property is a string in the "Purchase" class, and that it's what you want to base your auto-completion on:

var purchases = _aPurchaseManager.GetProductNameWithCategoryAndBrandName();

autoComplete.AddRange(purchases.Select(p => p.Name).ToArray());

As I don't see where else the error could be happening, I'll assume it's in your second line.

string[] productNameFromJoinedDatabase = _aPurchaseManager.GetProductNameWithCategoryAndBrandName();

I'll also assume that PurchaseManager.GetProductNameWithCategoryAndBrandName() returns type Purchase[] rather than string.

There's two ways to fix this, that functionally are the same, but performance-wise are a little different. The fast way:

Purchase[] purchases = _aPurchaseManager.GetProductNameWithCategoryAndBrandName();
string[] productNameFromJoinedDatabase = new string[purchases.Length];

for (int i = 0; i < purchases.Length; i++)
{
    productNameFromJoinedDatabase[i] = purchases[i].ProductName; // where ProductName is the name you're trying to get
}

The second way is a teeny bit slower, but prettier in my opinion. That's to use Linq;

using System.Linq;

...

string[] productNameFromJoinedDatabase =
    _aPurchaseManager.GetProductNameWithCategoryAndBrandName().Select(purchase => purchase.ProductName).ToArray();

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