public ProductInfo[] GetByIds(int[] productIDs) { Throws.IfArrayArgumentNullOrEmpty(productIDs, _ => productIDs); StringBuilder sb = new StringBuilder(); sb.Append("<items>"); foreach (var id in productIDs) { sb.AppendFormat("<item id=\"{0}\" />", id); } sb.Append("</items>"); using (SqlConnection connection = new SqlConnection(_connectionString)) { const string sql = SqlQuery; using (SqlCommand cmd = new SqlCommand(sql, connection)) { cmd.Parameters.Add((new SqlParameter("@xmlParameter", SqlDbType.NVarChar, -1) { Value = sb.ToString() })); connection.Open(); var list = new List <ProductInfo>(); try { // производим запрос - без этого не будет работать dependency using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { var item = new ProductInfo { Id = Convert.ToInt32(reader["Id"]), Updated = Convert.ToDateTime(reader["Updated"]), Alias = Convert.ToString(reader["Alias"]), Hash = Convert.ToString(reader["Hash"]), Title = Convert.ToString(reader["Title"]), LastPublishedUserId = reader["UserUpdatedId"] == DBNull.Value ? null : (int?)reader["UserUpdatedId"], LastPublishedUserName = reader["UserUpdated"].ToString() }; list.Add(item); } } return(list.ToArray()); } finally { connection.Close(); } } } }
public ProductInfo[] GetByIds(int[] productIDs) { Throws.IfArrayArgumentNullOrEmpty(productIDs, _ => productIDs); using (DpcModelDataContext context = _customer.DatabaseType == DatabaseType.Postgres ? GetNpgSqlDpcModelDataContext(_customer.ConnectionString) : GetSqlServerDpcModelDataContext(_customer.ConnectionString)) { var products = context.GetProducts(new ProductLocator { Language = _language, IsLive = _state, Version = 1, QueryFormat = _isJson ? "json" : "xml" }).Where(p => productIDs.Contains(p.DpcId) ).ToArray(); var list = new List <ProductInfo>(); // производим запрос - без этого не будет работать dependency foreach (var product in products) { var item = new ProductInfo { Id = product.DpcId, Updated = product.Updated, Alias = product.Alias, Hash = product.Hash, Title = product.Title, LastPublishedUserId = product.UserUpdatedId, LastPublishedUserName = product.UserUpdated }; list.Add(item); } return(list.ToArray()); } }