I want to select certain fields from my domain model, and group by a field within the domain model, and put the results into a ViewModel - however, I can't get the Linq syntax correct.
My Model is:
public class Offer
{
public int OfferId { get; set; }
public string Inclusions { get; set; }
public string Property { get; set; }
public bool IncludeInOffer { get; set; }
public Guid guid { get; set; }
public int NumPeople { get; set; }
public int NumNights { get; set; }
public DateTime ArrivalDate { get; set; }
public string CustomerName { get; set; }
public string EmailAddress { get; set; }
public DateTime OfferCreatedOn { get; set; }
public string Email { get; set; }
public string OfferReference { get; set; }
}
My ViewModel is:
public class OfferVMList
{
public string guid { get; set; }
public int NumPeople { get; set; }
public int NumNights { get; set; }
public DateTime ArrivalDate { get; set; }
public string CustomerName { get; set; }
public string EmailAddress { get; set; }
public DateTime OfferCreatedOn { get; set; }
public string Email { get; set; }
public string OfferReference { get; set; }
}
My Linq is:
OfferVMList item = db.Offers
.GroupBy(x => x.OfferReference)
.Select(ct => new OfferVMList
{
NumPeople = ct.NumPeople
})
.OrderBy(x => x.ArrivalDate);
However, the error message is:
System.Linq.IGrouping<string,FGBS.Models.Offer>' does not contain a definition for 'NumPeople' and no extension method 'NumPeople' accepting a first argument of type 'System.Linq.IGrouping<string,FGBS.Models.Offer>' could be found (are you missing a using directive or an assembly reference?)'
Can anyone see where I'm going wrong please?
Thank you
Check this:
var item = db.Offers
.OrderBy(x => x.ArrivalDate);
.GroupBy(x => x.OfferReference)
.Select(ct => ct.Select(x => new OfferVMList {
NumPeople = x.NumPeople
});
Related
I've two class Agens and OrdiniPuddy. There is a relationship among them by mean codAge field.
public class Agens
{
public string COD_CG { get; set; }
public string codAge { get; set; }
public string Agente { get; set; }
public string Societa { get; set; }
public DateTime PrimaTrasm { get; set; }
public DateTime UltimaTrasm { get; set; }
public int Destinazioni { get; set; }
public int Pezzi { get; set; }
public double Imponibile { get; set; }
public List<OrdiniPuddy> ordiniPuddy { get; set; }
}
public class OrdiniPuddy
{
public string codCli { get; set; }
public string Dest_ID { get; set; }
public string Ragsoc { get; set; }
public string Indirizzo { get; set; }
public string Localita { get; set; }
public string Prov { get; set; }
public string Regione { get; set; }
public string Calendario { get; set; }
public string Cap { get; set; }
public string codAgente { get; set; }
}
I've populated them with this code:
var results = connection.QueryMultiple(sql);
IEnumerable<Agens> agenti = results.Read<Agens>();
IEnumerable<OrdiniPuddy> ordiniP = results.Read<OrdiniPuddy>();
Last step is to passing ordiniP to agenti.ordiniPuddy by mean AddRange Linq method
like this
foreach (var item in agenti)
{
IEnumerable <OrdiniPuddy> ordini = ordiniP.Where(o => o.codAgente == item.codAge).ToList();
agenti.ordiniPuddy.AddRange((IEnumerable<Agens>)ordini);
}
but the compiler give me a error message on agenti.ordiniPuddy: 'type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?).
Where is the error, why I can't referencing agenti.ordiniPuddy?
My C# knowledge are at beginning.
Thank's
Fix your code
var ordini = ordiniP.Where(o => o.codAgente == item.codAge).ToList();
if(ordini!=null)
{
if( item.ordiniPuddy ==null) item.ordiniPuddy=new List<OrdiniPuddy>();
item.ordiniPuddy.AddRange(ordini);
}
I have two table like this -
public class Job
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime AddedTime { get; set; } = DateTime.Now;
public DateTime LastEdit { get; set; } = DateTime.Now;
public string Explanation { get; set; }
public string PhotoString { get; set; }
public bool isActive { get; set; } = true;
public int CompanyId { get; set; }
public Company Company { get; set; }
}
and company -
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Explanation { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string PhotoString { get; set; }
public bool isActive { get; set; } = true;
public int AppUserId { get; set; }
public AppUser AppUser { get; set; }
public List<Job> Jobs { get; set; }
}
I only want to get AppUserId from Company and all Jobs from every Company. I tried this and it gave me error.
using var context = new SocialWorldDbContext();
return await context.Jobs.Where(I => I.isActive == true && I.Company.isActive).Include(I=>I.Company.AppUserId).ToListAsync();
So my question is there any way I can get this data from parent?
Include adds whole entities to the output. To add just one property use Select, something like
context.Jobs
.Where(I => I.isActive == true && I.Company.isActive)
.Select(e => new {Job=e, CompanyAppUserId = e.Company.AppUserId})
.ToListAsync();
I'm trying to filter a list by the elements from another list, using the following code:
public JsonResult GetEquipamentosByFiliaisInstalacao(List<EmpresaFocusDTO> filiais)
{
var instalacoesFiltradasPorFiliais = _db.Instalacao
.Where(i => filiais.Any(s => s.Id == i.IdFilialFocus))
.ToList()
I'm getting the following error on the linq line:
Unable to create a constant value of type 'MasterCoin.DTO.Focus.EmpresaFocusDTO'. Only primitive types or enumeration types are supported in this context.
What do I have to do?
Here is the classes asked by #er-shoaib :
public class EmpresaFocusDTO
{
public int Id { get; set; }
public string Sigla { get; set; }
}
public partial class Instalacao
{
public int Id { get; set; }
public string Serie { get; set; }
public int Distancia { get; set; }
public int Deslocamento { get; set; }
public int IdEquipamentoFocus { get; set; }
public int IdClienteFocus { get; set; }
public bool Ativo { get; set; }
public string NomeEquipamentoFocus { get; set; }
public string NomeClienteFocus { get; set; }
public int IdFilialFocus { get; set; }
public int IdGrupoFocus { get; set; }
public string NomeFilialFocus { get; set; }
public string NomeGrupoFocus { get; set; }
public int IdRepresentanteFocus { get; set; }
public string NomeRepresentanteFocus { get; set; }
public string Uf { get; set; }
}
As the error says:
Only primitive types are allowed
You need to create a collection of just id columns and then use it in your linq query. Ef is not able to cater with your custom type collection.
Try this:
var ids = filiais.Select(s => s.Id).ToList();
var instalacoesFiltradasPorFiliais = _db.Instalacao
.Where(i => ids .Any(s => s == i.IdFilialFocus))
.ToList()
I want to check if some items are same in a list based on a item present in the list.
List<ProductDetailDTO> productDTOs;
The ProductDetailDTO is -
public class ProductDetailDTO
{
public int ProductId { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public byte[] Image { get; set; }
public string Description { get; set; }
public string Brand { get; set; }
public string GUID { get; set; }
public string VariantName { get; set; }
public string VariantValue { get; set; }
public decimal Price { get; set; }
}
Now, I want to display all VariantName and VariantValue with the same GUIDs together.
How can I achieve this?
try with this
productDTOs.GroupBy(x => x.GUID,(key,item) => new
{
VariantName= item.Select(y=>y.VariantName),
VariantValue = item.Select(y => y.VariantValue),
}).ToList()
I want to map one call to another, but I got exception all the time /An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code/
Here are my Source Classes:
public class Snippet
{
public Snippet()
{
this.Labels = new HashSet<Label>();
this.Commennts = new HashSet<Comment>();
}
public int Id { get; set; }
[Required]
public string Title { get; set; }
public string Description { get; set; }
[Required]
public string Code { get; set; }
public int LanguageId { get; set; }
public string UserId { get; set; }
public DateTime CreatedOn { get; set; }
public ICollection<Label> Labels { get; set; }
public ICollection<Comment> Commennts { get; set; }
public virtual Language Language { get; set; }
public virtual ApplicationUser User { get; set; }
}
public class Label
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Snippet> Snippets { get; set; }
}
Here are my Destination classes:
public class SnippetModels
{
public class Output
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime CreatedOn { get; set; }
public string Code { get; set; }
public string LanguageName { get; set; }
public string UserUsername { get; set; }
public IEnumerable<LabelModels.Output> Labels { get; set; }
//TODO Comments Labels
}
}
public class LabelModels
{
public class Output
{
public int Id { get; set; }
public string Name { get; set; }
}
}
public class HomeViewModel
{
public IEnumerable<SnippetModels.Output> Snippets { get; set; }
public IEnumerable<LabelModels.Output> Labels { get; set; }
}
And finally I tied everything that pop up in my mind but no success:
Mapper.CreateMap<Snippy.Models.Label, LabelModels.Output>();
Mapper.CreateMap<IEnumerable<Snippy.Models.Label>, IEnumerable<LabelModels.Output>>();
Mapper.CreateMap<Snippy.Models.Snippet, SnippetModels.Output>()
.ForMember(dest => dest.Labels, opt => opt.MapFrom(src => src.Labels));
Mapper.CreateMap<IEnumerable<Snippy.Models.Snippet>, IEnumerable<SnippetModels.Output>>();
var newestSnippetsDatabase = this.Data.Snippets
.All()
.OrderByDescending(s => s.CreatedOn)
.Take(HomeVisibleItemsCount)
.Select(s => s)
.ToList();
var homeScreenView = new HomeViewModel
{
Snippets = Mapper.Map<IEnumerable<SnippetModels.Output>>(newestSnippetsDatabase)
};