C# C linq到sql组多行
更新: 还有一张叫做位置特殊标志的表格。它将存储的位置ID和特殊标志配对。有必要在变量SpecialSignsLocation中生成结果,并获得以下结果:C# C linq到sql组多行,c#,sql,linq,C#,Sql,Linq,更新: 还有一张叫做位置特殊标志的表格。它将存储的位置ID和特殊标志配对。有必要在变量SpecialSignsLocation中生成结果,并获得以下结果: public IEnumerable<KeyValuePair<int, int>> SpecialSignsLocation = new List<KeyValuePair<int, int>>(); //json result ... SpecialS
public IEnumerable<KeyValuePair<int, int>> SpecialSignsLocation = new List<KeyValuePair<int, int>>();
//json result
...
SpecialSigns: {
BodyType: [1, 2],
Hair: [3, 1, 2],
SpecialSignsLocation: [[1,2], [3,5], [4,1]]
}
...
//query
SpecialSignsLocation = entity.persons_signs_location
.Where(c => c.PersonId == persons.Id)
.Select(s => new { s.Location, s.Sign})
.ToDictionary(l => l.Location, l => l.Sign)
相反,结果被复制6次
[
{
SpecialSigns: {
BodyType: [1, 2],
Hair: [3, 1, 2]
},
PersonName: "Aaa",
PersonLastName: "Bbb",
PersonPatronymic: "Ccc",
},
{
SpecialSigns: {
BodyType: [1, 2],
Hair: [3, 1, 2]
},
PersonName: "Aaa",
PersonLastName: "Bbb",
PersonPatronymic: "Ccc"
},
{
SpecialSigns: {
BodyType: [1, 2],
Hair: [3, 1, 2]
},
PersonName: "Aaa",
PersonLastName: "Bbb",
PersonPatronymic: "Ccc"
},
{
SpecialSigns: {
BodyType: [1, 2],
Hair: [3, 1, 2]
},
PersonName: "Aaa",
PersonLastName: "Bbb",
PersonPatronymic: "Ccc"
},
{
SpecialSigns: {
BodyType: [1, 2],
Hair: [3, 1, 2]
},
PersonName: "Aaa",
PersonLastName: "Bbb",
PersonPatronymic: "Ccc"
},
{
SpecialSigns: {
BodyType: [1, 2],
Hair: [3, 1, 2]
},
PersonName: "Aaa",
PersonLastName: "Bbb",
PersonPatronymic: "Ccc"
}
]
问题:如何组合特殊标识符,并将它们带到阵列中?请尝试以下操作。连接导致笛卡尔积
public List<object> GetPerson(int id)
{
var query = (from persons in entity.persons where persons.Id.Equals(id)
select new Person
{
PersonName = persons.PersonName,
PersonLastName = persons.PersonLastName,
PersonPatronymic = persons.PersonPatronymic,
SpecialSigns = new PersonSpecialSigns()
{
BodyType = entity.persons_signs_body_type
.Where(c => c.PersonId == persons.Id)
.Select(c => c.PersonBodyType),
Hair = entity.persons_signs_hair
.Where(c => c.PersonId == persons.Id)
.Select(h => h.PersonHair)
}
});
return query.ToList<object>();
}
我要试试这个。但是,我几乎可以肯定,如果您使用DefaultIfEmpty,您将为您的结果带来更大的影响。所以,这就是为什么你可能要复制所有东西。忽略这一点,你将拥有我认为你正在寻找的内在连接
var query = from persons in entity.persons where persons.Id == id
select new Person
{
PersonName = persons.PersonName,
PersonLastName = persons.PersonLastName,
PersonPatronymic = persons.PersonPatronymic,
SpecialSigns = new PersonSpecialSigns()
{
BodyType = entity.persons_signs_body_type
.Where(i => i.PersonId == id)
.Select(i => i.PersonBodyType),
Hair = entity.persons_signs_hair
.Where(i => i.PersonId == id)
.Select(i => i.PersonHair)
}
};
另外,作为一项建议,我将首先使用FirstOrDefault检查此人是否存在,然后填充该类
var personData = entity.persons.FirstOrDefault(i = i.Id == id);
if(personData != null)
{
var person = new Person
{
PersonName = personData.PersonName,
PersonLastName = personData.PersonLastName,
PersonPatronymic = personData.PersonPatronymic,
SpecialSigns = new PersonSpecialSigns()
{
BodyType = entity.persons_signs_body_type
.Where(i => i.PersonId == personData.Id)
.Select(i => i.PersonBodyType),
Hair = entity.persons_signs_hair
.Where(i => i.PersonId == personData.Id)
.Select(i => i.PersonHair)
}
};
//continue with the code
}
这里的速记linq应该很好用
public List<object> GetPerson(int id)
{
var query = entity.persons.First(x=> x.Id == id).Select(x=> new Person
{
PersonName = x.PersonName,
PersonLastName = x.PersonLastName,
PersonPatronymic = x.PersonPatronymic,
SpecialSigns = new PersonSpecialSigns
{
BodyType = entity.persons_signs_body_type
.Where(y => y.PersonId == x.Id)
.Select(y => y.PersonBodyType),
Hair = entity.persons_signs_hair
.Where(y => y.PersonId == x.Id)
.Select(y => y.PersonHair)
}
});
return query.ToList<object>();
}
假设您使用的是实体框架,并且您的实体看起来类似于: Person.cs
public int Id { get; set; }
public string PersonName { get; set; }
public string PersonLastName { get; set; }
public string PersonPatronymic { get; set; }
public ICollection<PersonsSignsHair> PersonsSignsHair { get; set; }
public ICollection<PersonsSignsBodyType> PersonsSignsBodyType { get; set; }
PersonSignsByType.cs
public int Id { get; set; }
public int PersonId { get; set; }
public string PersonBodyType { get; set; }
你有这样的背景:
public DbSet<PersonsSignsHair> PersonsSignsHair { get; set; }
public DbSet<PersonsSignsBodyType> PersonsSignsBodyType { get; set; }
public DbSet<Person> People { get; set; }
请把你得到的结果也包括进来。@wdosanjos我想他已经很清楚了:结果重复了6次如果你只需要一条记录,你可以做第一个或默认
public int Id { get; set; }
public int PersonId { get; set; }
public string PersonHair { get; set; }
public int Id { get; set; }
public int PersonId { get; set; }
public string PersonBodyType { get; set; }
public DbSet<PersonsSignsHair> PersonsSignsHair { get; set; }
public DbSet<PersonsSignsBodyType> PersonsSignsBodyType { get; set; }
public DbSet<Person> People { get; set; }
entity.People.Where(x => x.Id == id).Include(y => y.PersonsSignsHair).Include(z => z.PersonsSignsBodyType).FirstOrDefault();