简体   繁体   中英

LINQ Select From One Generic List to Assign Value to Another

While looping through records in one list, I want to find a corresponding line in another list containing more information for that item. I am getting a conversion error when trying to use LINQ to get this information from the first list.

public class qtyAvail
{
    public string itemNumber;
    public string qtyAv;
}

public class variance
{
    public string siteID;
    public string itemNumber;
    public string varQty;
}

    public void saveVariance(Context context)
    {
        var settings = PreferenceManager.GetDefaultSharedPreferences(context);
        var siteID = settings.GetString("siteID", null);
        updateInv(siteID);

        var inst = new InventoryApp();
        List<variance> ilist = new List<variance>();
        List<qtyAvail> avail = new List<qtyAvail>();
        SqliteDataReader dr;

        var connection = new SqliteConnection("Data Source=" + dbPath);
        connection.Open();
        var c = connection.CreateCommand();
        c.CommandText = "Select ItemNmbr, OnHnd - Alloc From PartInfo";
        dr = c.ExecuteReader();

        while (dr.Read())
        {
            qtyAvail qa = new qtyAvail();
            qa.itemNumber = dr[0].ToString();
            qa.qtyAv = dr[1].ToString();
            avail.Add(qa);
        }
        dr.Close();
        c.CommandText = "Select * From Items";
        dr = c.ExecuteReader();

        while (dr.Read())
        {
            variance v = new variance();
            v.siteID = siteID;
            v.itemNumber = dr[0].ToString();
            v.varQty = from q in avail where q.itemNumber == dr[1].ToString() select q.qtyAv;
            ilist.Add(v);
        }
        dr.Close();
        connection.Close();

        inst.saveVariance(siteID, ilist.ToArray());
    }

When I build the solution I get

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

On the line where I use LINQ to assign v.varQty . I am sure there is some simple thing I am missing but just can't find the right cast, conversion, etc. I also realize that I could easily just perform the select on the Sqlite table to get the information rather than using lists, but I am trying to figure all this LINQ stuff out so I want to get it to work this way too.

v.varQty = from q in avail where q.itemNumber == dr[1].ToString() select q.qtyAv;

Your query returns an IEnumerable<string> and not a single string -since varQty is a string use FirstOrDefault() here:

v.varQty = (from q in avail where q.itemNumber == dr[1].ToString() select q.qtyAv).FirstOrDefault();

Or shorter in dot notation:

v.varQty = avail.Where(q => q.itemNumber == dr[1].ToString()).FirstOrDefault();

from q in avail where q.itemNumber == dr[1].ToString() select q.qtyAv;

above linq query returns a collection of string. You cannot assign a collection to your String type variable

You should instead use FirstOrDefault() which will return only one record from that enumerable collection

Try this

(from q in avail where q.itemNumber == dr[1].ToString() select q.qtyAv).FirstOrDefault();

The line you have specified:

v.varQty = from q in avail where q.itemNumber == dr[1].ToString() select q.qtyAv;

Tries to assign a list of strings to a single string, so it fails.

You need to get a single item and assign that. You have several options - using First() or FirstOrDefault() which work as you would expect. Or use Single() which would throw an exception if there isn't exactly one item in the list.

In this example, I have use FirstOrDefault :

v.varQty = (from q in avail where q.itemNumber == dr[1].ToString()
           .FirstOrDefault();

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