C# 将连接值与mvc中的对象进行比较
这是MyPerson index.cshtml的输出。我不知道如何在index.cshtml表中加入sql linq,有什么想法吗C# 将连接值与mvc中的对象进行比较,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,这是MyPerson index.cshtml的输出。我不知道如何在index.cshtml表中加入sql linq,有什么想法吗 name allapartments active john 1, 2, 3 no mary 1 yes 对象公寓 id name 1 LA downtown apts 2 NYC downtown apts 希望输出: name allapartments
name allapartments active
john 1, 2, 3 no
mary 1 yes
对象公寓
id name
1 LA downtown apts
2 NYC downtown apts
希望输出:
name allapartments active
john LA downtown apts, NYC downtown apts no
mary LA downtown apts yes
个人控制员
public ActionResult Index()
{
var apartments = db.Apartments;
var result = from item in Persontable
where apartments.All(x => item.allapartments.Contains(x))
select item;
}
我想到了这个
void Main()
{
var personTables = new List<Persontable>{
new Persontable{Name = "John", AllApartments = new List<int>{1, 2, 3}, Active = false},
new Persontable{Name = "Mary", AllApartments = new List<int>{1}, Active = true},
};
var apartments = new List<Apartment>{
new Apartment { Id = 1, Name = "LA downtown apts"},
new Apartment { Id = 2, Name = "NYC downtown apts"},
};
var result = personTables.Select( p => new PersontableAparment{
Name = p.Name,
AllApartmentsNames = apartments.Where(a => p.AllApartments.Contains(a.Id)).Select(x => x.Name).ToList(),
Active = p.Active,
});
}
public class Persontable
{
public string Name { get; set;}
public List<int> AllApartments { get; set;}
public bool Active {get; set;}
}
public class Apartment
{
public int Id {get; set;}
public string Name {get; set;}
}
public class PersontableAparment
{
public string Name { get; set; }
public List<string> AllApartmentsNames { get; set; }
public bool Active { get; set; }
}
是Persontable表中的数据。请注意,我正在使用PersontableApartment
类对您想要的输出进行建模。如果你觉得有用,请把它当作公认的答案。我想出了这个
void Main()
{
var personTables = new List<Persontable>{
new Persontable{Name = "John", AllApartments = new List<int>{1, 2, 3}, Active = false},
new Persontable{Name = "Mary", AllApartments = new List<int>{1}, Active = true},
};
var apartments = new List<Apartment>{
new Apartment { Id = 1, Name = "LA downtown apts"},
new Apartment { Id = 2, Name = "NYC downtown apts"},
};
var result = personTables.Select( p => new PersontableAparment{
Name = p.Name,
AllApartmentsNames = apartments.Where(a => p.AllApartments.Contains(a.Id)).Select(x => x.Name).ToList(),
Active = p.Active,
});
}
public class Persontable
{
public string Name { get; set;}
public List<int> AllApartments { get; set;}
public bool Active {get; set;}
}
public class Apartment
{
public int Id {get; set;}
public string Name {get; set;}
}
public class PersontableAparment
{
public string Name { get; set; }
public List<string> AllApartmentsNames { get; set; }
public bool Active { get; set; }
}
是Persontable表中的数据。请注意,我正在使用PersontableApartment
类对您想要的输出进行建模。如果你觉得有用,请把它当作公认的答案