KeithL
KeithL

Reputation: 5594

After getting distinct list, using the results in a query

I originally got help with getting a distinct list from a larger list with multiple nodes to group. I think that worked.

I now need help on how to use that list.

Here is my code:

var LOE = results.Body
                 .getEntitiesResponse
                 .getEntities
                 .listOfEntities
                 .Select(x=>new string[]{x.entityIdentification.DUNS,x.entityIdentification.DUNSPlus4})
                 .Distinct();

foreach (var d in LOE)
{
    using (OleDbConnection conn = new OleDbConnection(cm.ConnectionString))
    {
        using (OleDbCommand cmd = new OleDbCommand())
        {
            cmd.CommandText = "sam.DeleteRecordsToBeUpdated";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@a", d.DUNS);  //This is my problem area
            cmd.Parameters.AddWithValue("@b", d.DUNSPlus4); //This is my problem area
            cmd.Connection = conn;

            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

Can someone help me with how to use the new object created in the first line?

Maybe I am not setting the first line properly? I can't seem to use the object as I am trying to.

Upvotes: 1

Views: 61

Answers (2)

haldo
haldo

Reputation: 16711

You could create a class to map the values into:

public class DunsMapping()
{
    public string Duns { get; set; }
    public string DunsPlus4 { get; set; }

    public DunsMapping(string duns, string duns4)
    {
        Duns = duns;
        DunsPlus4 = duns4;
    }
}

Then the linq would become:

var LOE = results.Body
             .getEntitiesResponse
             .getEntities
             .listOfEntities
             .Select(x=>new DunsMapping(x.entityIdentification.DUNS,
                                        x.entityIdentification.DUNSPlus4))
             .Distinct();

Or, if you needed to return a list of distinct entities you can use GroupBy:

var LOE = results.Body
             .getEntitiesResponse
             .getEntities
             .listOfEntities
             .GroupBy(g => new { g.DUNS, g.DUNSPlus4 })
             .Select(g => g.First());

which will return IEnumerable<YourEntity>.

Upvotes: 1

Vikhram
Vikhram

Reputation: 4404

Your problem is with the first statement

var LOE = results.Body.getEntitiesResponse.getEntities.listOfEntities
          .Select(x=>new string[]{x.entityIdentification.DUNS,x.entityIdentification.DUNSPlus4})
          .Distinct();

You should have it like below

var LOE = results.Body.getEntitiesResponse.getEntities.listOfEntities
            .Select(x => new {
                x.entityIdentification.DUNS,
                x.entityIdentification.DUNSPlus4
            }).Distinct();

In your case, you are selecting an array, instead of an anonymous class

Upvotes: 3

Related Questions