我有一个模特:
public class Transaction
{
public Guid ID { get; set; }
public Guid CategoryID { get; set; }
public List<Guid> Tags { get; set; }
}
public class Category
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
public class Tag
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
我有一个Model对象的ModelRepository。我有VieModels:
public class TransactionViewModel
{
public Guid ID { get; set; }
public CategoryViewModel Category { get; set; }
public List<TagViewModel> Tags { get; set; }
}
public class CategoryViewModel
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
public class TagViewModel
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
现在我需要使用linq从List<TransactionViewModel>
创建List <Transaction>
。我是怎么做的:
VM.Categories = ModelRepository.Categories.Select(p => new CategoryViewModel(p)));
VM.Tags = ModelRepository.Tags.Select(p => new TagViewModel(p)));
var trans = from t in ModelRepository.Transactions
join c in VM.Categories on t.CategoryID equals c.ID
select new TransactionViewModel()
{
...,
Category = c
};
VM.Transactions = trans.ToList();
现在,当我更改CategoryViewModel
中的任何VM.Categories
时,我会立即对TransactionViewModel.Category
中的所有VM.Transactions
进行更改。问题 - 如何填写TransactionViewModel.Tags
?最好用LINQ。在TagViewModel
更改任何VM.Tags
时,我必须立即对TransactionViewModel.Tags
中的所有VM.Transactions
进行更改。