logo
down
shadow

How can I merge the results of a group by Linq-to-XML query?


How can I merge the results of a group by Linq-to-XML query?

Content Index :

How can I merge the results of a group by Linq-to-XML query?
Tag : chash , By : Nicholas Hunter
Date : November 28 2020, 09:01 AM

To fix the issue you can do It turns out Falanor's ideas was right, I just had a small error in the way I wrote the solution that caused it not to work. The method call to UnionWith should have been:
removed.UnionWith(group.items.Select(el => el.Parent).Where(el => !el.Equals(group.target)));
where g.Select(p => p.Parent).Distinct().Count() > 1

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

LINQ query to group results into an array


Tag : chash , By : user105769
Date : March 29 2020, 07:55 AM
will be helpful for those in need Given: , This looks straightforward, unless I missed your meaning:
IEnumerable<GroupItem> linqQuery = detailList
    .GroupBy(i => i.A)
    .Select(g => new GroupItem() 
    { 
        A = g.Key, 
        AllBsWithinA = g.Select(i => i.B).ToList() 
    });

Linq to Entities group query giving list of results in each group


Tag : chash , By : yatil
Date : March 29 2020, 07:55 AM
it should still fix some issue If you want to get List of Ids for each group then you have to select x.Select(r => r.Id) like:
var result = Items.GroupBy(x => new { x.Size, x.Type })
                  .Select(x => new
                    {
                        Key = x.Key,
                        Ids = x.Select(r => r.Id)
                    });

Linq group query gives wrong results


Tag : chash , By : amorican
Date : March 29 2020, 07:55 AM
will be helpful for those in need In order for GroupBy to produce a grouping, the object by which you group must have an override of GetHashCode and Equals, or you need to provide a suitable equality comparer to GroupBy.
Your Stock does not override GetHashCode/Equals, and your GroupBy query does not use a custom equality comparer, so you get unexpected results.
public class Stock {
    public string Name {get; set;}
    public List<int> Values {get; set;}
    public float Price {get; set;}
    public override bool Equals(object obj) {
        if (obj == this) return true;
        var other = obj as Stock;
        if (other == null) return false;
        return Name.Equals(other.Name)
            && Price == other.Price
            && Values.SequenceEqual(other.Values);
    }
    public override int GetHashCode() {
        return Name.GetHashCode()
             + Price.GetHashCode()
             + Values.Sum();
    }
}

LINQ merge 2 query results


Tag : chash , By : Chris Woods
Date : March 29 2020, 07:55 AM
To fix this issue The datatable has 5 columns , This works perfectly well for your code.,
DataRow[] dr1 = dtt.Select("Course = 'Math' AND Month > 10");
        var result_one = dr1.AsEnumerable()
            .GroupBy(r => new { Name = r.Field<string>("Name"), Class = r.Field<string>("Class") })
            .Select(g => new
            {
                Name = g.Key.Name,
                Class = g.Key.Class,
                Max = g.Max(r => r.Field<int>("Score")),
                Max_Month = g.FirstOrDefault(gg => gg.Field<int>("Score") == g.Max(r => r.Field<int>("Score"))).Field<int>("Month"),
            }
            ).Distinct().ToList();


DataRow[] dr2 = dtt.Select("Course = 'Chem'");
        var result_two = dr2.AsEnumerable()
            .GroupBy(r => new { Name = r.Field<string>("Name"), Class = r.Field<string>("Class") })
            .Select(g => new
            {
                Name = g.Key.Name,
                Class = g.Key.Class,                    
                Chem_Max = g.Max(r => r.Field<int>("Score")),
                Chem_Max_Month = g.FirstOrDefault(gg => gg.Field<int>("Score") == g.Max(r => r.Field<int>("Score"))).Field<int>("Month"),
            }
            ).Distinct().ToList();
var lstLeftJoin = (from a in result_one
                           join b in result_two
                           on new { a.Name, a.Class } equals new { b.Name, b.Class }
                           into gj
                           from subpet in gj.DefaultIfEmpty()
                           select new { a.Name, a.Class, Math_Max_Month = a.Max_Month, Math_Max = a.Max, Chem_Max_Month = (subpet == null ? 0 : subpet.Chem_Max_Month), Chem_Max = (subpet == null ? 0 : subpet.Chem_Max) }).ToList();
  var lstRightJoin = (from a in result_two
                            join b in result_one
                            on new { a.Name, a.Class } equals new { b.Name, b.Class }
                            into gj
                            from subpet in gj.DefaultIfEmpty()
                            select new { a.Name, a.Class, Math_Max_Month = (subpet == null ? 0 : subpet.Max_Month), Math_Max = (subpet == null ? 0 : subpet.Max), a.Chem_Max_Month, a.Chem_Max }).ToList();
var lstUnion = lstLeftJoin.Select(s => new { Name = s.Name, Class = s.Class, Math_Max_Month = s.Math_Max_Month, Math_Max = s.Math_Max, Chem_Max_Month = s.Chem_Max_Month, Chem_Max = s.Chem_Max }).Union(lstRightJoin.Select(s => new { Name = s.Name, Class = s.Class, Math_Max_Month = s.Math_Max_Month, Math_Max = s.Math_Max, Chem_Max_Month = s.Chem_Max_Month, Chem_Max = s.Chem_Max })).OrderBy(o => o.Name).ThenBy(c => c.Class).ToList();
Name  Class    Math_Max_Month    Math_Max  Chem_Max_Month   Chem_Max
Alex   C1      12                90        null             null
Alex   C2      11                91        12               92
Bob    C1      null              null      12               97
Bob    C2      12                94        null             null

How to group DataTable results using Linq query expression


Tag : chash , By : kameel
Date : March 29 2020, 07:55 AM
this will help I have the following DataTable , You need insert String.Join in your code:
var BooksList = from Book in BookTable.AsEnumerable()
                        group Book by Book.Field<string>("BookCategory") into g
                        let list = g.ToList()
                        select new
                        {
                            BookCategory = g.Key,

                            BookNames = string.Join(" ; ",list.Select(x => x.Field<string>("BookName").ToString()))
                        };
Related Posts Related QUESTIONS :
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • How to convert Word document created from template by OpenXML into MemoryStream?
  • How can I make a single slider that changes the color of an object?
  • Remap JSON parameter in c#
  • What is the difference between "this ref" and "ref this" when talking about C# 7.2 ref extension met
  • Convert OpenSSL encryption into native C#
  • Accessing Properties in Razor Pages
  • How to get SOAP element value
  • Projection after Group
  • C# error cannot convert sytem.text.regularexpressions.match to string
  • Issues with Save/Load System in a Text Based Adventure game made with ScriptableObjects in Unity
  • VS2019 MSBuild.exe - ASP .Net MVC project fails to publish when using PublishProfile, but works when using OutDir parame
  • Does <pages validateRequest="false" /> in Web.config still matter?
  • How to send new request to redirect URL with new access token
  • Attempt to invoke virtual method on a null object reference Xamarin LockScreen
  • "The attribute names could not be inferred from bind attribute 'bind-value'" exception in Blazor
  • How to fix ''System.ArgumentException" in c#?
  • C#. Ref returning delegate for ref extension method
  • Swashbuckle Swagger generate an actual guid
  • Trying to make thousands of database calls simultaneously - they stack instead of running concurently
  • run mstest from cmd using testcontainer
  • How to make M:N (many-to-many) relationship where both M and N are the same entities?
  • Consume COM DLL from multiple clients
  • Convert json string response representing UCHAR array to Byte array
  • Why does this LinqPad program produce different results on the second run?
  • ServiceStack how to use MaxLoginAttempts feature
  • What does "late-bound access to the destination object" mean?
  • How to load appsettings.json inside a Static class based on deploy environment so that I can use in other class librarie
  • dynamically add images to view
  • Are static extension methods on User object safe?
  • Process thousands of database calls simultaneously
  • Trying to decrypt a string with public key(not private) using bouncycastle in .net
  • How to retrieve the name of an usb device
  • Query many-to-many related data using Entity Framework Core
  • ASP.NET MVC : how to find the view from a controller?
  • Inconsistent behaviour using Entity Framework for ASP.NET MVC app vs windows service
  • AutoFixture Customize vs Build
  • How to change textbox location at the click of a mouse in live mode?
  • How can I deserialize a XML file without defining the parent node?
  • asp.net core how decrease class coupling
  • ASP.NET Core Razor Pages Routing With parameters
  • String Escaping With $ Interpolation and @ Encoding
  • ConfigurationManager in ASP.NET WebAPI tested with HttpServer
  • Top down shooting in Unity 2D don't work as I want it to
  • Use own redirectUri when the Authentication expires
  • Using "foreach" in string array c#
  • "Object reference not set to an instance of an object" for create multiply models on one view
  • Get data passed trought pushAsync
  • Selector expression dynamic on IQueryable
  • Logut with SignInManager not calling method
  • Exit event handler with MessageBox OK button
  • Can not load view from Custom Renderer Xamarin Forms
  • Looking for a .Net ORM
  • Cost of SortedDictionary.Count
  • All possible combinations of n items selected randomly from a set of x items (algorithm)
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com