简体   繁体   中英

LINQ in WCF returning repeating results

I've got a LINQ query in a WCF service that runs and returns the correct number of results that I'm looking for, but repeats the first result 25 times instead of showing me all 25 different records.

The weird thing is that when I take the SQL query that it generates from the debugger and plug it into SQL Management studio, I get the correct results.

I have tried refreshing the view I'm querying from the edmx, and I've tried rewriting the query a few different ways, but I'm starting to run out of ideas. I've included some of the code below. Any suggestions would be helpful. Thanks!

try
        {
            using (Entities db = new Entities())
            {
                var qInventory = db.vw_Web_Store_Inventory_Live
                                    .Where(qi => qi.Sku_Number == inputSKU)
                                    .ToList();

                resultPInventory.SKU = inputSKU;
                resultPInventory.StoreInventory = new List<StoreItem>();

                foreach (var qi in qInventory)
                {
                    resultPInventory.StoreInventory.Add(new StoreItem
                    {
                        StoreNum = qi.Store_Number,
                        Quantity = qi.Curr_Inv
                    });
                }
            }
        }
        catch (Exception e)
        {
            log.Error("[" + e.TargetSite + "] | " + e.Message);
        }

        log.Info("ProductInventory(" + inputSKU + ") returned " + resultPInventory.StoreInventory.Count + " results");

        return resultPInventory;

As Gert's link in the comments points out, sometimes LINQ may do that if your primary keys are not set up well, or if there are multiple rows with no unique values in your database.

This link also shows a similar problem.

The solution, other than rewriting your database columns (although that would be better on the long run) with better primary / unique keys, is to select specific values anonymously (later you can assign them easily):

var qInventory = db.vw_Web_Store_Inventory_Live
                    .Where(qi => qi.Sku_Number == inputSKU)
                    .Select(qi => new { qi.Store_Number, qi.Curr_Inv })
                    .ToList();

resultPInventory.SKU = inputSKU;
resultPInventory.StoreInventory = new List<StoreItem>();

foreach (var qi in qInventory)
{
    resultPInventory.StoreInventory.Add(new StoreItem
    {
        StoreNum = qi.Store_Number,
        Quantity = qi.Curr_Inv
    });
}

Of course this won't be the best way if you later need to use qInventory for other things. (In that case you can just select more fields)

PS, here is a way to shorten your code, but I am not sure if LINQ to Entities will allow it, so test it first:

resultPInventory.SKU = inputSKU;
resultPInventory.StoreInventory = db.vw_Web_Store_Inventory_Live
                                  .Where(qi => qi.Sku_Number == inputSKU)
                                  .Select(qi => new StoreItem { StoreNum = qi.Store_Number, Quantity = qi.Curr_Inv })
                                  .ToList();

在添加新的StoreItem()之前,先将其分配给变量。

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