簡體   English   中英

C#linq to sql group multiple rows

[英]C# linq to sql group multiple rows

更新:

還有一個名為Location特殊標志的表。 它將存儲的位置ID和特殊符號配對。 有必要在變量SpecialSignsLocation中生成結果並獲得以下內容:

    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)

我寫了一個請求,但我過了異常:

表達式LINQ to Entities無法識別方法System.Collections.Generic.Dictionary

共有3個表:Persons,PersonsSignsHair,PersonsSignsBodyType。 一個人可能有很多特殊標志:

圖片

public List<object> GetPerson(int id)
{
   var query = (from persons in entity.persons where persons.Id.Equals(id)
               join signs_body_type in entity.persons_signs_body_type 
                 on persons.Id equals signs_body_type.PersonId into _signs_body_type
                      from signs_body_type in _signs_body_type.DefaultIfEmpty()

               join signs_hair in entity.persons_signs_hair 
                on persons.Id equals signs_hair.PersonId into _signs_hair
                      from signs_hair in _signs_hair.DefaultIfEmpty()

               select new Person
                      {
                          PersonName = persons.PersonName,
                          PersonLastName = persons.PersonLastName,
                          PersonPatronymic = persons.PersonPatronymic,
                          SpecialSigns = new PersonSpecialSigns()
                          {
                              BodyType = _signs_body_type.Select(c => c.PersonBodyType),
                              Hair = _signs_hair.Select(h => h.PersonHair)
                          }
                 });

     return query.ToList<object>();
}

查詢之后,結果將轉換為JSON。 輸出,我希望得到以下結果:

[
    {
        SpecialSigns:  {
            BodyType:  [1, 2],
            Hair:  [3, 1, 2]
        },

        PersonName: "Aaa",
        PersonLastName: "Bbb",
        PersonPatronymic: "Ccc",

    },
]

相反,結果重復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"

    }
]

問題:如何組合特殊標識符,並將它們帶入陣列?

請嘗試以下方法。 join's是導致笛卡爾積。

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() ,你將為你的結果帶來一個Left Outer Join 所以,這就是為什么你可能會復制一切。 省略這個,你就會有你的內心聯系(我認為你在尋找)

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>();
}

假設您正在使用Entity Framework,您的實體看起來與以下類似:

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; }

PersonsSignsHair.cs

public int Id { get; set; }
public int PersonId { get; set; }
public string PersonHair { get; set; }

PersonsSignsBodyType.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; }

您可以加載具有此類相關實體的Person

entity.People.Where(x => x.Id == id).Include(y => y.PersonsSignsHair).Include(z => z.PersonsSignsBodyType).FirstOrDefault();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM