Concatenate several records in a chain to show it in only one? LINQ

1

I have a table called Technologys and each Technology can have one or more resolutions. What I want to do is show all the resolutions that a technology has in the same field.

Example

Tabla de tecnologias
Nombre   Descripcion   Resoluciones
1        prueba        140,33,32

I need to do it with linq and I have been told that the key is in a String Join or in an Agrete using two queries but I can not get it.

This is my code:

  var Technology1 = (from  tTechnology in Context.Technology

                                                 join tUsers in Context.Users on tTechnology.EnableBy equals tUsers.UserId into collection
                                                 from subCase in collection.DefaultIfEmpty()
                                                 join tUsers2 in Context.Users on tTechnology.LastChangeBy equals tUsers2.UserId into collection2
                                                 from subCase2 in collection2.DefaultIfEmpty()
                                                 join tUsers3 in Context.Users on tTechnology.DisableBy equals tUsers3.UserId into collection3
                                                 from subCase3 in collection3.DefaultIfEmpty()
                                                 join tResolution in Context.Resolution on tTechnology.TechnologyId equals tResolution.TechnologyId 
                                                 into collection4
                                                 from subCase4 in collection4.DefaultIfEmpty()                  
                                                 where tTechnology.DisableDate == null



                             //orderby 

            select new
            {
                tTechnologyTechnologyId = tTechnology.TechnologyId,
                tTechnologyName = tTechnology.Name,
                tTechnologyDescription = tTechnology.Description,
                tCityName = tTechnology.City.Name,
                tStateName = tTechnology.City.State.Name,
                tCountryName = tTechnology.City.State.Country.Name,
                tResolutions = (from tR in Context.Resolution
                                 where tR.TechnologyId == tTechnology.TechnologyId
                                 select tR.Measure
                               ),

                tEnableBy = (subCase.Name == null ? null : subCase.Name) + "" + (subCase.FirstLastName == null ? null : subCase.FirstLastName) + " " + (subCase.SecondLastName == null ? null : subCase.SecondLastName),
                                     tEnableDate = tTechnology.EnableDate,
                                     tLastChangeDate = tTechnology.LastChangeDate,
                                     tLastChangeBy = (subCase2.Name == null ? null : subCase2.Name) + " " + (subCase2.FirstLastName == null ? null : subCase2.FirstLastName) + " " + (subCase2.SecondLastName == null ? null : subCase2.SecondLastName),
                                     tDisableDate = tTechnology.DisableDate,
                                     tDisableBy = (subCase3.Name == null ? null : subCase3.Name) + " " + (subCase3.FirstLastName == null ? null : subCase3.FirstLastName) + " " + (subCase3.SecondLastName == null ? null : subCase3.SecondLastName),

            });



            var Technology = (from tTec in Technology1
                              select new
                              {
                                  tTechnologyTechnologyId = Technology1.ToList().Select(t => t.tTechnologyTechnologyId).ToString(),
                                  tTechnologyName = Technology1.ToList().Select(t => t.tTechnologyName).ToString(),
                                  tTechnologyDescription = Technology1.ToList().Select(t => t.tTechnologyDescription).ToString(),
                                  tCityName = Technology1.ToList().Select(t => t.tCityName).ToString(),
                                  tStateName = Technology1.ToList().Select(t => t.tStateName).ToString(),
                                  tCountryName = Technology1.ToList().Select(t => t.tCountryName).ToString(),
                                  tResolutions = String.Join(",", tTec.tResolutions),
                              }
                            );

In the second query, what I want to do is take some values from the first one, and also, gather all the resolutions that a technology has in tResolutios.

At the end the second query, called Technology, is the one I return.

Grace.

    
asked by E. Antonio 14.07.2016 в 21:49
source

2 answers

1

You should use tTec to access the properties

var Technology = (from tTec in Technology1
              select new
              {
                  tTechnologyTechnologyId = tTec.tTechnologyTechnologyId,
                  tTechnologyName = tTec.tTechnologyName,
                  tTechnologyDescription = tTec.tTechnologyDescription,
                  tCityName = tTec.tCityName,
                  tStateName = tTec.tStateName,
                  tCountryName = tTec.tCountryName,
                  tResolutions = String.Join(",", tTec.tResolutions),
              });

Since the property Measure of type long you should convert it to string in the first query linq

 tResolutions = (from tR in Context.Resolution
                             where tR.TechnologyId == tTechnology.TechnologyId
                             select tR.Measure.ToString()
                           ).ToArray(),
    
answered by 14.07.2016 / 22:04
source
0

In the end I could, everything was in that the first Query had to be a list

 var Technology = Technology1.ToList().AsQueryable().Select( x =>  new 
            {
                tTechnologyTechnologyId = x.tTechnologyTechnologyId,
                tTechnologyName = x.tTechnologyName,
                tTechnologyDescription = x.tTechnologyDescription,
                tCityName = x.tCityName,
                tStateName = x.tStateName,
                tCountryName = x.tCountryName,
                tResolutions = String.Join(",", x.tResolutions),
                tEnableBy = x.tEnableBy,
                tEnableDate = x.tEnableDate,
                tLastChangeDate = x.tLastChangeDate,
                tLastChangeBy = x.tLastChangeBy,
                tDisableDate = x.tDisableDate,
                tDisableBy = x.tDisableBy,
            });
    
answered by 14.07.2016 в 23:42