简体   繁体   English

Linq:一个匿名类型不能有多个同名的属性

[英]Linq: An anonymous type cannot have multiple properties with the same name

How can i use GroupBy in linq expression using the same name?, I have the following code to get data from multiple entities.我如何在 linq 表达式中使用同名的 GroupBy?,我有以下代码从多个实体获取数据。 I have RP and SubParty tables both have a Name field and I want to display both but linq giving me the following error我有 RP 和 SubParty 表都有一个名称字段,我想同时显示这两个表,但 linq 给我以下错误

An anonymous type cannot have multiple properties with the same name一个匿名类型不能有多个同名的属性

var rp = await context.RP.AsNoTracking()
        .Include(rp => rp.SubPartyNav)
        .Include(rp => rp.partyNav)
        .Where(rp => rp.ProductID == request.ID)
        .GroupBy(rpSp => new
        {
                rpSp.partyNav.PartyName,
                rpSp.SubPartyNav.PartyNav.PartyName })
        .Select(r => new RspDTO
        {
                PartyName = r.Key.PartyName,
                SubPartyName = r.Key.PartyName, })
        .ToListAsync();

Just explicitly give the properties names:只需明确给出属性名称:

        .GroupBy(rpSp => new
        {
                PartyName = rpSp.partyNav.PartyName,
                SubPartyName = rpSp.SubPartyNav.PartyNav.PartyName })
        .Select(r => new RspDTO
        {
                PartyName = r.Key.PartyName,
                SubPartyName = r.Key.SubPartyName, })

Also, it looks more like you want .Distinct() instead of .GroupBy() , since you're only using the group key:此外,它看起来更像是你想要.Distinct()而不是.GroupBy() ,因为你只使用组键:

        .Select(rpSp => new
        {
                PartyName = rpSp.partyNav.PartyName,
                SubPartyName = rpSp.SubPartyNav.PartyNav.PartyName })
        .Distinct()
        .Select(r => new RspDTO
        {
                PartyName = r.PartyName,
                SubPartyName = r.SubPartyName, })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq Groupby具有相同名称的多个列(匿名类型不能具有具有相同名称的多个属性) - Linq Groupby multiple columns with the same name (An anonymous type cannot have multiple properties with the same name) C# JSON 匿名类型 - 具有相同名称的多个属性 - C# JSON Anonymous Type - Multiple properties with the same name Linq查询从对象B获取具有相同名称和对象类型A的属性 - Linq query to get the properties from object B which have the same name and type of object A 将LINQ Project属性转换为包含Contains的新匿名类型 - LINQ Project properties into a new anonymous type with Contains 为 LINQ 匿名类型的一个或多个属性分配名称有哪些优点/缺点? - What are the advantages/disadvantages of assigning a name to one or more of the properties a LINQ anonymous type? Linq 错误无法对匿名类型的变量进行操作 - Linq Error Cannot Operate on Variables of Anonymous Type 带有匿名类/属性的LINQ - LINQ with anonymous class/properties EFCore - 如何将多个导航属性设置为相同类型? - EFCore - How to have multiple navigation properties to the same type? 选择匿名类型时,LINQ包含嵌套属性 - LINQ include nested properties when selecting anonymous type INotifyPropertyChanged与具有相同名称的属性 - INotifyPropertyChanged with Properties that have the same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM