У меня есть объект, который PropertyValues reperesent свойства продукта:Сплит коллекция рамок LINQ сущность
public enum Property { Color = 1, Brand, Size }
public class PropertyValue
{
public int PropertyValueId { get; set; }
public Property PropertyId { get; set; }
public string Value { get; set; }
public ICollection<Product> Products { get; set; }
public PropertyValue()
{
Products = new HashSet<Product>();
}
}
Потому что мы имеем конец сходящейся последовательности свойств продукта я создал перечислимую свойство соблюдающим свойства. Я пытаюсь добиться следующего результата - разбить коллекцию в зависимости от свойств. Где значения являются другими Словарь
Dictionary<Property, Dictionary<int, string>> dict = new Dictionary<Property, Dictionary<int, string>>()
{
new KeyValuePair<Property, Dictionary<int, string>>()
{
{
Property.Color,
new KeyValuePair<int, string>()
{
{ 5, "white" }, // ProperyValueId, Value
{ 6, "green"}
}
}
}
}
Я был осмотрен GroupBy и SelectMany, но не нашел способ. Сейчас я следующее:
var query = await db.PropertyValues
.OrderBy(prop => prop.PropertyId)
.Where(prop => prop.PropertyId == Property.Brand)
.ToDictionaryAsync(prop => prop.PropertyValueId, prop => prop.Value);
Dictionary<Property, Dictionary<int, string>> properties = new Dictionary<Property, Dictionary<int, string>>();
properties.Add(Property.Brand, query);
Если вернуть JSON. Но сначала нужно получить последовательность. Json должен выглядеть следующим образом:
[{
{"colors": [{"5", "Black"}, {"7", "White"}]},
{"brands": [{"78", "Q&Q"}, {"24", "Adidas"}]},
}]
Можете ли вы представить небольшую текстовую (таблицу) информацию о том, что вы ожидаете в результате? – sam
см. Edit, я добавил, как нужно искать json. – unsafePtr
См. Ответ – sam