0

Say for instance I have a List<Person> where each Person has a dozen or so properties. The first ten properties are all set when the list is generated.

Later I need to populate the data for the last two properties on each item in the list.

I have a stored proc that returns PersonId, Property11, Property12 and I'd like to use Linq to take the values from an IDataReader and populate the values on the correct Person from the list, matched on PersonId obviously.

I'm starting with something like: (This is not the actual code I'm using, but the structure is similar.)

public void GetAdditionalProps( List<IPersonData> people)
{
    var peopleIds = String.Join( ",", people.Select(x => x.ID));

    using (var conn = Database.GetConnection(Database.PeopleDB))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = "spGetPeopleData" ;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.AddParameterWithValue( "@Ids" , peopleIds);

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    var pd = people.First(p => p.ID.Equals(reader["PersonId" ]));
                    pd.HairColor = reader["HairColor" ].ToString();
                    pd.EyeColor = reader["EyeColor" ].ToString();
                }
            }
        }
    }
}

But I wonder if I can use Linq to populate the data for each list item without having to find each one individually?


What is the Correct Way To Add a :staging group to My Gemfile

I have remotes set up on Heroku for production and staging.

On staging I have set the app's envs to include:

RACK_ENV=staging
RAILS_ENV=staging

I would like to be able to specify a staging group in my Gemfile in the same way I can currently use production, test or assets:

group :staging do
  gem "example", "~> 0.9"
end

I understand how to add custom groups. From my application.rb:

  groups = {
    assets: %w(development test)
  }
  Bundler.require(:security, :model, :view, *Rails.groups(groups))

But how do I add a group that is only loaded in staging?

I've tried without success:

  groups = {
    assets: %w(development test),
    staging: %(staging)
  }
  Bundler.require(:security, :model, :view, *Rails.groups(groups))
4

1 回答 1

0

你可以做这样的事情......

虽然这里没有太多使用“强制”Linq ......

也许加入比每次调用 First() 都快...

public Tuple<int,string,string> GetAdditionalData(List<IPersonData> people)
{
    string peopleIds = String.Join( ",", people.Select(x => x.ID));

    using (var conn = Database.GetConnection(Database.PeopleDB))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = "spGetPeopleData" ;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.AddParameterWithValue( "@Ids" , peopleIds);

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    yield return new Tuple<int,string,string>(
                        reader["PersonId" ],
                        reader["HairColor" ],
                        reader["EyeColor" ]);
                }
            }
        }
    }
}

public void Enrich(List<IPersonData> people)
{
    var dataToEnrich = from person in people
                       join additional in GetAdditionalData(people)
                           on additional.Item1 equals person.ID
                       select new { Person = person, 
                                    HairColor = additional.Item2, 
                                    EyeColor = additional.Item3 };

    foreach(var enrichment in dataToEnrich)
    {
        enrichment.Person.HairColor = enrichment.HairColor;
        enrichment.Person.EyeColor = enrichment.EyeColor ;
    }
}
于 2013-10-30T21:22:48.313 回答