Add non existing item to list - linq

I am trying to fill a list with objects which haven't been added yet by random to a list. So I loop rInt times through a list and want to pick randomly objects and add them to the list if they does not already exist:
collectionList = new List<CollectionSccmCM>();
Random r = new Random();
int rInt = r.Next(0, 5);
for(int i=0; i<=rInt; i++){
collectionList.Add(_context.CollectionApplications.OrderBy(x => Guid.NewGuid()).Where(x => collectionList.Any(y => y.CollectionID !=(x.collection_id.ToString()))).Select(x => new CollectionSccmCM(){CollectionID= x.collection_id.ToString(), Name=x.collection_name}).FirstOrDefault());
}
I seems that I have a mistake in the orderby and where part, but I cannot figure out the error. When I put a toList between I dont receive any syntax error anymore, but also doesn't work.
Any tip what I am doing wrong?
Thanks
Edit:
I did a mistake and had to use contains, but still not working:
collectionList.Add(_context.CollectionApplications.OrderBy(x => Guid.NewGuid()).Where(x => collectionList.Any(y => !y.CollectionID.Contains(x.collection_id.ToString()))).Select(x => new CollectionSccmCM(){CollectionID= x.collection_id.ToString(), Name=x.collection_name}).FirstOrDefault());
Edit:
Got it working with a select, but not so happy with it and dont understand why the otherone wasnt working.
collectionList.Add(_context.CollectionApplications.OrderBy(x => Guid.NewGuid()).Where(x => !collectionList.Select(y => y.CollectionID).ToList().Contains(x.collection_id.ToString())).Select(x => new CollectionSccmCM(){CollectionID= x.collection_id.ToString(), Name=x.collection_name}).FirstOrDefault());

I would strongly suggest you consider adding some whitespace to your LINQ. I would break your first example down as follows:
collectionList.Add(
_context.CollectionApplications
.OrderBy(x => Guid.NewGuid())
.Where(x => collectionList.Any(y => y.CollectionID !=(x.collection_id.ToString())))
.Select(x => new CollectionSccmCM() {
CollectionID = x.collection_id.ToString(),
Name = x.collection_name
}).FirstOrDefault()
);
Looking at your Where call, you are including in the possible elements to add, those elements where any of the collection IDs doesn't match (collectionList.Any(y => ... )). That's all of them (unless you only have one element in collectionList).
You probably want to use All instead of Any -- where all of the collection IDs don't match:
.Where(x => collectionList.All(y => y.CollectionID != x.collection_id.ToString()))

Related

Using Linq, How can I properly append multiple Where clauses so that they appear in the appropriate order?

The order I would like the end result to appear in is Exact Matches first given a input string, followed by other matches that are Contains for a given field. I tried to approach this in a very rudimentary way as shown here in this example:
var raw = Model.SearchResults.Where(m => m.EffectiveDateTime != null).OrderBy(m => m.EffectiveDateTime).ToList();
var exact = raw.Where(m => m.IssueNumber.ToLower() == Model.SearchText.ToLower());
var contains = raw.Where(m => m.IssueNumber.ToLower().Contains(Model.SearchText.ToLower()));
var list = exact.Union(contains);
This approach seems like it'd be a really bad way to do this. In fact, the Union portion seems to effectively crash my application. Is there an opposite to Intersection which would give me the remaining results outside the Exact matches that I could then append to a final list so that the order would be Exact Matches followed by StartsWith matches followed finally by Contains matches in that descending order?
To answer your original question, you can use a temporary expression to classify the match types, then order by the match type and other criteria, and it will translate to SQL as well:
var st = Model.SearchText.ToLower();
var list = Model.SearchResults.Where(m => m.EffectiveDateTime != null)
.Select(m => new {
m,
im = m.IssueNumber.ToLower()
})
.Select(mim => new {
mim.m,
Rank = mim.im == st ? 1 : mim.im.StartsWith(st) ? 2 : mim.im.Contains(st) ? 3 : 4
})
.Where(mr => mr.Rank < 4)
.OrderBy(mr => mr.Rank)
.ThenBy(mr => mr.m.EffectiveDateTime)
.Select(mr => mr.m)
.ToList();
I did the double Select to emulate let from fluent syntax, which I think is a bit clearer than lambda syntax in this case:
var lisx = (from m in Model.SearchResults
where m.EffectiveDateTime != null
let im = m.IssueNumber.ToLower()
let Rank = im == st ? 1 : im.StartsWith(st) ? 2 : im.Contains(st) ? 3 : 4
where Rank < 4
orderby Rank, m.EffectiveDateTime
select m)
.ToList();
Also, if you do the whole query in the database, the ToLower is likely unnecessary, as the default for SQL is probably to be case-insensitive anyway.
Actually, I went back to the drawing board and figured it out. This is a little bit better for me and returns the results I needed.
var list = Model.SearchResults
.Where(e => e.A.ToLower().Contains(Model.SearchText.ToLower()))
.GroupBy(d => new { d.A, d.B, d.C})
.OrderBy(x => x.Key.A)
.ThenBy(x => x.Key.B)
.ThenBy(x => x.Key.C)
.Select(x => new
{
A= x.Key.A,
B= x.Key.B,
C= x.Key.C
})
.ToList();

how do i add dynamic highlight fields in nest for elasticsearch?

i want to be able to dynamically add fields for highlighting in elasticsearch using nest. currently it looks like it's not a able to be iterated in any fashion.
i've tried iterating within the .OnFields function in order to produce a list of .OnField functions, but it says it's not iterable.
in this example, i want to dynamically add 'artist' and 'title' and add/remove others based on user input. is this possible?
s.Highlight(h => h
.OnFields(f => f
.OnField("artist")
.OnField("title")
.PreTags("<em>")
.PostTags("</em>")
));
Highlight takes an array of Action<HighlightFieldDescriptor<T>>. You are only passing a single Action<HighlightFieldDescriptor<T>> and calling OnField multiple times on it, which keeps replacing the last value.
It should be this instead:
s.Highlight(h => h
.OnFields(
f => f.OnField("artist").PreTags("<em>").PostTags("</em>"),
f => f.OnField("title").PreTags("<em>").PostTags("</em>")
));
From the code in your follow up post, here's a solution using LINQ:
s.Highlight(h => h
.OnFields(
SearchFields(searchDescriptor.SearchModifier).Select(x => new Action<HighlightFieldDescriptor>(f => f.OnField(x))).ToArray()
));
i realized i had confused a couple of types:
HighlightFieldDescriptor and HighlightDescriptor. sorry. here's my implementation (so i can mark as answered)
s.Highlight(h => h
.OnFields(f =>
GetFieldsHighligthDescriptor(searchDescriptor, f)
)
);
private void GetFieldsHighligthDescriptor(SearchQueryDescriptor searchDescriptor, HighlightFieldDescriptor<Product> f)
{
foreach (var b in SearchFields(searchDescriptor.SearchModifier))
{
f.OnField(b);
}
}
EDIT
actually, this isn't working because it's only return the last entry in my SearchFields array... back to the drawing board?

How to order a LinQ result set created by a list of ID and order it use existing order of the list

My code is something like this:
List<int> IDs = new List<int> {9,7,38, 23}
IQueryable<Post> pp = myDataEntity.Posts.Where(p=>IDs.Contains(p.ID));
How can I explain that I want to sort pp by the order of ID in the IDs? Thank you.
You can order by List.IndexOf:
IQueryable<Post> orderedPosts myDataEntity.Posts
.Select(p => new { Post=p, Index=IDs.IndexOf(p.ID) })
.Where(x => x.Index >= 0)
.OrderBy(x => x.Index)
.Select(x => x.Post);
Assuming you want to keep the order of ID you have in IDs and not actually the ordered verison of it, this is how you can do it:
var pp = IDs.Select(x=> myDataEntity.Posts.Where(p=>p.ID == x))
.SelectMany(x=>x);
Using IDs as your external collection, you get to keep the ordering of the elements in it.
use OrderByDescending
IQueryable<Post> pp = myDataEntity.Posts.Where(p=>IDs.Contains(p.ID))
.OrderByDescending(p => p.ID)

Finding n-Most Popular Using Linq

How should I structure a Linq query to return a List or Ienumerable of the most popular Tags in my db (I am using EF4.1 by the way).
Currently I have:
var tagsListing = db.Tags
.GroupBy(q => q.Name)
.OrderByDescending(gp => gp.Count())
.Take(5)
.Select();
I think I am part of the way there, but I am unsure of how to structure the Select statement...
Your Select call could look like this:
.Select(gp => gp.Key)
That will give you an IEnumerable<string> of your most popular tags (assuming that Name is a string).
Assuming you want the name and the count, just:
.Select(g => new { Name = g.Key, Count = g.Count() });
EDIT: If you want the complete tags as well, you could use:
.Select(g => new { Tags = g, Count = g.Count() })
which would give you a sequence of groups of tags, all with the same name within a group. Or you might only want the first tag within each group, e.g.
.Select(g => g.First())
It's not clear what a Tag consists of, or what exactly you want in the results.
You've written a perfectly workable query and do not need to call .Select
IQueryable<IGrouping<string, Tag>> tagsListing = db.Tags
.GroupBy(q => q.Name)
.OrderByDescending(gp => gp.Count())
.Take(5);
List<IGrouping<string, Tag>> results = tagListing.ToList();
You probably want to select the names like this:
.Select(gp => gp.Key);

Find the max value in a grouped list using Linq

I have a linq expression that returns transactions in groups. Each transaction has a numerical value and I now need to know what is the highest value from all the transactions returned. This value is held in a field called TransactionId
Here is the expression I am using to get the grouped list.
var transactions = ctx.MyTransactions
.Where (x => x.AdapterId == Id)
.GroupBy(x => x.DeviceTypeId);
I now need to write an expression that works on the “transactions” grouped list to find the “max” of the TransactionId field. I’ve tried different ideas but none seem to work with the grouped results. I’m new to linq so I’m not sure how to do this.
Have you tried finding the maximum in each group and then finding the maximum of that over all groups?
int max = transactions.Max(g => g.Max(t => t.TransactionId));
Or you could just query the database again:
int max = ctx.MyTransactions
.Where(x => x.AdapterId == Id)
.Max(t => t.TransactionId);
This will give you the max in each group
var transactionIds = ctx.MyTransactions
.Where (x => x.AdapterId == Id)
.GroupBy(x => x.DeviceTypeId,
g => new {
DeviceTypeId = g.Key,
MaxTransaction = g.Max(x => x.TransactionId)
});

Resources