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.
Your query returns an
IEnumerable<string>and not a single string -sincevarQtyis a string useFirstOrDefault()here:Or shorter in dot notation: