logo
down
shadow

is there a better way to write this frankenstein LINQ query that searches for values in a child table and orders them by


is there a better way to write this frankenstein LINQ query that searches for values in a child table and orders them by

By : tingtingtwice
Date : October 28 2020, 08:00 PM
hope this fix your issue I have a table of Users and a one to many UserSkills table. I need to be able to search for users based on skills. This query takes a list of desired skills and searches for users who have those skills. I want to sort the users based on the number of desired skills they posses. So if a users only has 1 of 3 desired skills he will be further down the list than the user who has 3 of 3 desired skills. , I think that should do the trick:
code :


Share : facebook icon twitter icon
How to write a single LINQ to XML query to iterate through all the child elements & all the attributes of the child

How to write a single LINQ to XML query to iterate through all the child elements & all the attributes of the child


By : Daniel Booth
Date : March 29 2020, 07:55 AM
I hope this helps you . You could just iterate through all the elements and retrieve the attributes for each:
code :
var allDescendants = myElement.DescendantsAndSelf();
var allDescendantsWithAttributes = allDescendants.SelectMany(elem =>
    new[] { elem }.Concat(elem.Attributes().Cast<XContainer>()));

foreach (XContainer elementOrAttribute in allDescendantsWithAttributes)
{
    // ...
}
How to write an SQL query that searches for a group of words from another table?

How to write an SQL query that searches for a group of words from another table?


By : user3699966
Date : March 29 2020, 07:55 AM
Hope this helps I don't know MS SQL, but most SQL implementations have a 'LIKE' equivalent. So in that case, you could join the two tables and use LIKE in the JOIN condition.
code :
SELECT Id as link
FROM Posts P JOIN SpellingMistakes S
ON P.Body LIKE '%'+S.wrong+'%'
SELECT Id as link
FROM (SELECT * FROM Posts WHERE Id<=10000) P 
JOIN 
SpellingMistakes S
ON P.Body LIKE '%'+S.wrong+'%'

SELECT Id as link
FROM (SELECT * FROM Posts WHERE Id<=20000 and Id>10000) P 
JOIN    
SpellingMistakes S
ON P.Body LIKE '%'+S.wrong+'%'
How to write Linq query that pulls top three rows from child table

How to write Linq query that pulls top three rows from child table


By : Anthony Senior
Date : March 29 2020, 07:55 AM
I wish this help you Perhaps you can use a group join, which is the join into clause in query syntax:
code :
(from reqHeader in db.ReqHeaders
 join reqLineItem in db.ReqLineItems 
     on reqHeader.ReqID equals reqLineItem.ReqId into reqLineItems
 select new 
        {  
            reqHeader.ReqHeaderId,
            Nomenclatures = reqLineItems.Select(x => x.Nomenclature).Take(3)
        })
.AsEnumerable()
.Select(x => new
             {
                 x.ReqHeaderId,
                 Nomenclatures = String.Join(", ", x.Nomenclatures.ToArray())
             })
LINQ Query - Display customers with their number of orders including the customer with no orders

LINQ Query - Display customers with their number of orders including the customer with no orders


By : Korantin LEVEQUE
Date : March 29 2020, 07:55 AM
help you fix your problem You basically need to do a LEFT JOIN between your Customer table and Customer Order table and then do a group by on that result to count the orders for each customer.
Assuming you have a class like this
code :
public class CustomerOrder
{
  public int CustomerId { set; get; }
  public int? OrderId { set; get; }
}
var usersWithCount = (from c in db.Customers
    join o in db.Orders on c.CustomerId equals o.CustomerId 
    into result
    from sub in result.DefaultIfEmpty()
         select new CustomerOrder{  OrderId= sub!=null ? sub.OrderId :(int?) null,
                                    CustomerId = u.CustomerId }
) // Here we have the left join result.
.GroupBy(g => g.CustomerId , (k, orders) => new { 
                    CustomerId = k, 
                    OrderCount = orders.Count(t=>t.OrderId !=null)}).ToList();
How can I simplify this LINQ query that searches for keywords in strings and orders them by relevance?

How can I simplify this LINQ query that searches for keywords in strings and orders them by relevance?


By : Frunk
Date : March 29 2020, 07:55 AM
hope this fix your issue Let's say I have some MyObjects and each of them have a Description property. I have a list of keywords that I want to use to search through the MyObject list. I want to order them in descending order, by the number of keywords that each of their Description contains. , Try that:
code :
    var objects = new[]{
                    new MyObject{Description = "Foo Bar"},
                    new MyObject{Description = "Foo Boo"},
                    new MyObject{Description = "Foo Bee"},
                    new MyObject{Description = "Bar Bee"},
                    new MyObject{Description = "Boo Bee"},
                };
                var keywords = new[] { "Foo", "Bar" };
                var results = objects
                    .GroupBy(x => keywords.Where(
                                          keyword => x.Description.Contains(keyword) 
                                          ).Count()
                    )
                    .Where(x => x.Key > 0) // discard no matches
//                    .OrderByDescending(x => x.Count()) // order by mathing objects count
                    .OrderByDescending(x => x.Key)
//                   .ToDictionary(x => x.Key, x => x.ToArray())
                     .Select(x => new {Count = x.Key, Objects = x.ToArray()}).ToList(); // or create anonymous type
                    ;
Related Posts Related Posts :
  • How to use Selenium Grid with C#?
  • What is the best way to download files via HTTP using .NET?
  • How to get files from a device using USB
  • Given a user's SID, how do I get their userPrincipalName?
  • NHibernate mapping in Asp.Net using MySql
  • Why do some cookies have a '.' before the domain?
  • C# SqlDataReader = null?
  • InvalidCastException for two Objects of the same type
  • "The parameters dictionary contains a null entry for parameter" - How to fix?
  • Font family name from font file
  • What is the best way to generate KML files in C#?
  • How can I receive mail using .NET?
  • How to send raw data over a network?
  • meaning of '+='
  • Object reference not set to an instance of an object #5
  • C# Create "wireframe"/3D "map"
  • How to change size of database
  • Serialization problem
  • Using unmanaged code from managed code
  • Are there any bindings between .NET and TK
  • error with linq join
  • VB.NET equivalent to C# var keyword
  • Accessing object properties from string representations
  • Inheritance issue
  • C# timer won't tick
  • How to retrieve items from a database c#
  • Sending mail using SmtpClient in .net
  • Tag problem c# listbox
  • How to know if the Form App open or not c#
  • C# XPath id() not working?
  • Load PDF from Memory ASP.Net
  • C# ListView with a ProgressBar
  • Getting the right WPF dispatcher in a thread
  • How to create Pivot table using C#?
  • how to download a file from remote server using asp.net
  • Binding files in C#?
  • Copy one object to another
  • How to post on Google Buzz?
  • Generic <T> how cast?
  • Set global hotkeys using C#
  • Change the key being pressed with C#
  • Uploading Large Files
  • How do I get the duration of a video file using C#?
  • how to create instance for a generic type in c#
  • Drag and drop rectangle in C#
  • RSA Encryption C#
  • Title=
  • What is meant by Web Services?
  • The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via
  • Check if server exists
  • time interval in c#
  • Extracting a sub-string in C#
  • C# - Programmatically Log-off and Log-on a user
  • c# array vs generic list
  • TCPClient in C# (Error)
  • How can I know if a file has been changed in .NET C#?
  • New to C# and trying to use a global variable
  • Convert RGB color to CMYK?
  • Tesseract.NET in C#
  • Is it possible to Update Sharepoint List Without "ID"?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk