嵌套列表的LINQ内连接

时间:2017-05-06 12:22:09

标签: c# linq join

我有一个模特:

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进行更改。

0 个答案:

没有答案