Entity Framework - Select distinct in - c#

I have a table called Tag with a column called Label and a column called AuctionId. I also have an array of strings which are search terms. I want to write some Linq to Entities code which will give me a distinct list of AuctionIds where the the Label matches one of the search terms. Here is the pseudocode for this:
return a list of unique AuctionIds where Label is in searchTerms
How can this be done?

You can use Contains() on the list.
List<String> AuctionIDs = (from tagItem in Tags
where searchItems.Contains(tagItem.Label)
select tagItem.AutionID).Distinct().ToList();

Using Lambda notation for clarity, this breaks down to a number of functions in sequence as follows:
IEnumerable<Int32> DistinctIds = TagTable.Where(x => searchTerms.Contains(x.Label)).Select( x => x.AuctionId).Distinct()
Without going too far into the Lambda syntax, the key features here are:
.Where(x => searchTerms.Contains(x.Label)) - this will select out only rows where the searchTerms collection contains the Label value for that row
.Select( x => x.AuctionId) - return out only the integer AutionId values rather than the full record
.Distinct() - does just what it says on the tine
Hope this helps

Related

Linq select where entity property value matches value of property of any item in another List

Using Linq in C#, how do I select items from an list of objects of one entity type, where the value of a property of any of the objects matches the value of a property of any object in in a list containing objects of a different entity type? I'm looking for a real expression using fluent syntax that performs the function of the following Pseudocode (Entity A and Entity B aren't linked by keys)
MyContext.ListOfEntityA.Where(a => ListOfEntityB.Contains(ListOfEntityB.Property.Value == a.Value))
To clarify, if the collections contain objects that look like this:
ListOfEntityA
-------------
EntityA_Object.Property = 1
EntityA_Object.Property = 2
ListOfEntityB
-------------
EntityB_Object.Property = 2
Then the expression should return the 2nd item in ListOfEntityA
Try this out, It will work now.
MyContext.ListOfEntityA.Where(a => ListOfEntityB.Exists(b => b.Property.Value == a.Property.Value));
You could use a LINQ join expression to join the two lists on the matching property, filtering out all the elements without matching results.The result should be the matching elements from both lists as an IEnumerable result.
ListOfEntityA
.Join(ListOfEntityB, l => l.Property, r => r.Property, (a, b) => new { EntityAObject = a, EntityBObject = b });
ListOfEntityA.Where(a => ListOfEntityB.Any(b => b.Property == a.Property))
Any checks whether there is a match with an item in ListOfEntityB or not, and Where returns the objects in ListOfEntityA for which a match was found. See live:
https://dotnetfiddle.net/rbOJg5

Linq challenge: converting this piece of code from method chain to standard Linq

The challenge is about converting from method chain to standard linq a piece of code full of group by.
The context
To fully understand the topic here you can read the original question (with class definitions, sample data and so on): Linq: rebuild hierarchical data from the flattened list
Thanks to #Akash Kava, I've found the solution to my problem.
Chain method formulation
var macroTabs = flattenedList
.GroupBy(x => x.IDMacroTab)
.Select((x) => new MacroTab
{
IDMacroTab = x.Key,
Tabs = x.GroupBy(t => t.IDTab)
.Select(tx => new Tab {
IDTab = tx.Key,
Slots = tx.Select(s => new Slot {
IDSlot = s.IDSlot
}).ToList()
}).ToList()
}).ToList();
But, for sake of knowledge, I've tried to convert the method chain to the standard Linq formulation but something is wrong.
What happens is similar to this..
My attempt to convert it to Linq standard syntax
var antiflatten = flattenedList
.GroupBy(x => x.IDMacroTab)
.Select(grouping => new MacroTab
{
IDMacroTab = grouping.Key,
Tabs = (from t in grouping
group grouping by t.IDTab
into group_tx
select new Tab
{
IDTab = group_tx.Key,
Slots = (from s in group_tx
from s1 in s
select new Slot
{
IDSlot = s1.IDSlot
}).ToList()
}).ToList()
});
The result in LinqPad
The classes and the sample data on NetFiddle:
https://dotnetfiddle.net/8mF1qI
This challenge helped me to understand what exactly returns a Linq Group By (and how prolix is the Linq syntax with Group By).
As LinqPad clearly shows a Group By returns a List of Groups. Group is a very simple class which has just one property: a Key
As this answer states, from definition of IGrouping (IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable) the only way to access to the content of the subgroups is to iterate through elements (a foreach, another group by, a select, ecc).
Here is shown the Linq syntax formulation of the method chain.
And here is the source code on Fiddle
But let's go on trying to see another solution:
What we usually do in SQL when we do a Group By is to list all the columns but the one which have been grouped. With Linq is different.. it still returns ALL the columns.
In this example we started with a dataset with 3 'columns' {IDMacroTab, IDTab, IDSlot}. We grouped for the first column, but Linq would return the whole dataset, unless we explicitly tell him..

Group by Linq setting properties

I'm working on a groupby query using Linq, but I want to set the value for a new property in combination with another list. This is my code:
var result = list1.GroupBy(f => f.Name)
.ToList()
.Select(b => new Obj
{
ClientName = b.Name,
Status = (AnotherClass.List().Where(a=>a.state_id=b.????).First()).Status
})
I know I'm using a group by, but I'm not sure of how to access the value inside my bcollection to compare it with a.state_id.
This snippet:
Status = (AnotherClass.List().Where(a=>a.state_id=b.????).First()).Status
I've done that before but months ago I don't remember the syntax, when I put a dot behind b I have acces only to Key and the Linq Methods... What should be the syntax?`
Issue in your code is happening here:
a=>a.state_id=b.????
Why ?
Check type of b here, it would be IGrouping<TKey,TValue>, which is because, post GroupBy on an IEnumerable, you get result as IEnumerable<IGrouping<TKey,TValue>>
What does that mean?
Think of Grouping operation in the database, where when you GroupBy on a given Key, then remaining columns that are selected need an aggregation operation,since there could be more than one record per key and that needs to be represented
How it is represented in your code
Let's assume list1 has Type T objects
You grouped the data by Name property, which is part of Type T
There's no data projection so for a given key, it will aggregate the remaining data as IEnumerable<T>, as grouped values
Result is in the format IEnumerable<IGrouping<TK, TV>>, where TK is Name and TV represent IEnumerable<T>
Let's check out some code, break your original code in following parts
var result = list1.GroupBy(f => f.Name) - result will be of type IEnumerable<IGrouping<string,T>>, where list1 is IEnumerable<T>
On doing result.Select(b => ...), b is of type IGrouping<string,T>
Further you can run Linq queries on b, as follows:
b.Key, will give access to Name Key, there's no b.Value, for that your options could be following or any other relevant Linq operations:
a=>b.Any(x => a.state_id == x.state_id) or // Suuggests if an Id match in the Collection
a=>a.state_id == b.FirstOrDefault(x => x.state_id) //Selects First or default Value
Thus you can create a final result, from the IGrouping<string,T>, as per the logical requirement / use case

Retrieving non-duplicates from 2 Collections using LINQ

Background: I have two Collections of different types of objects with different name properties (both strings). Objects in Collection1 have a field called Name, objects in Collection2 have a field called Field.
I needed to compare these 2 properties, and get items from Collection1 where there is not a match in Collection2 based on that string property (Collection1 will always have a greater or equal number of items. All items should have a matching item by Name/Field in Collection2 when finished).
The question: I've found answers using Lists and they have helped me a little(for what it's worth, I'm using Collections). I did find this answer which appears to be working for me, however I would like to convert what I've done from query syntax (if that's what it's called?) to a LINQ query. See below:
//Query for results. This code is what I'm specifically trying to convert.
var result = (from item in Collection1
where !Collection2.Any(x => x.ColumnName == item.FieldName)
select item).ToList();
//** Remove items in result from Collection1**
//...
I'm really not at all familiar with either syntax (working on it), but I think I generally understand what this is doing. I'm struggling trying to convert this to LINQ syntax though and I'd like to learn both of these options rather than some sort of nested loop.
End goal after I remove the query results from Collection1: Collection1.Count == Collection2 and the following is true for each item in the collection: ItemFromCollection1.Name == SomeItemFromCollection2.Field (if that makes sense...)
You can convert this to LINQ methods like this:
var result = Collection1.Where(item => !Collection2.Any(x => x.ColumnName == item.FieldName))
.ToList();
Your first query is the opposite of what you asked for. It's finding records that don't have an equivalent. The following will return all records in Collection1 where there is an equivalent:
var results=Collection1.Where(c1=>!Collection2.Any(c2=>c2.Field==c1.Name));
Please note that this isn't the fastest approach, especially if there is a large number of records in collection2. You can find ways of speeding it up through HashSets or Lookups.
if you want to get a list of non duplicate values to be retained then do the following.
List<string> listNonDup = new List<String>{"6","1","2","4","6","5","1"};
var singles = listNonDup.GroupBy(n => n)
.Where(g => g.Count() == 1)
.Select(g => g.Key).ToList();
Yields: 2, 4, 5
if you want a list of all the duplicate values then you can do the opposite
var duplicatesxx = listNonDup.GroupBy(s => s)
.SelectMany(g => g.Skip(1)).ToList();

Order Subgrouping in Linq Query

I have a problem with a Linq query. I have a query formatted like the following which returns a result set that is grouped by the top level and is ordered by the top level strings alphabetically. I want to return a result set that is ordered by both the top level strings (so the group by clause is ordered alphabetically) and the sub level strings are also in alphabetical order (so each sub group is also alphabetical). What I'm expecting is something like the following:
A
A
B
C
B
A
B
C
My query is something like the following:
records.results
.Where(table => string.Compare(table, expected) == 0)
.OrderByDescending(table => table.name)
.GroupBy(table => table.name);
.OrderBy(group => group.Key);
I expect the OrderByDescending statement to change the ordering of the individual records in the groupby clause but it isn't effecting anything. I am receiving ordered results for the groups though. Any help is appreciated!
Your problem is that your final OrderBy statement is ordering the groups themselves. What you want to do is order the elements of each group. Try:
records.results
.Where(table => string.Compare(table, expected) == 0)
.OrderByDescending(table => table.name)
.GroupBy(table => table.name);
.Select(g => g.OrderByDescending(element => element.Name);
I'm not sure of the name of the property by which you want to order the groups is, but here I'm assuming it is Name.
The key here is to remember that IGrouping is itself an IEnumerable. See the documentation of IGrouping here and note that it implements IEnumerable, so we can call OrderByDescending on each group in your IEnumerable of IGroupings, which is the return type of Enumerable.GroupBy(documented here).
Since OrderByDescending will return an IEnumerable (instead of an IGrouping), this operation will lose the Key property on each group. If you need that, then you might want to simply select an anonymous type to keep the key. Replace the final Select with:
.Select(g => new { g.Key, elements = g.OrderByDescending(element => element.Name) });
Alternatively, you could write some logic that would intantiate an IGrouping here by following the directions here.

Categories

Resources