linq

How to check if the value exist in the database table ignoring null in LINQ C# [duplicate]

青春壹個敷衍的年華 提交于 2021-01-29 15:47:16
问题 This question already has answers here : Why is EF generating SQL queries with unnecessary null-checks? (3 answers) Closed 11 months ago . I want to ignore null when the query check the value exist in database table in C# using LINQ and Lambda expression. Below line is checking null as well. When it finds null then ignore it. var nmcExist = db.AspNetUsers.Any(a => a.NMC_Number== model.NMC_Number); 回答1: You might be looking for 2 options: If null , return true .Any(a => a.NMC_Number == null ||

Linq counting second grouping and counting without grouping

别来无恙 提交于 2021-01-29 13:55:45
问题 I'm trying to build a summary query that i will be using for statistics. i have a dataTable with the folowing columns (approx 18000 rows) : Artist / Album / file_path (one for each song) / rating / each artist has 1 or several album with has songs and each songs have a rating I want to have the following result : For each artist ID (more reliable than the artist name), the total number of albums, the total number of songs, and the total number of ratings equal to 5. Artist x / #album / #songs

Organization Chart - Group by Manager to show all employees under him

喜你入骨 提交于 2021-01-29 13:42:53
问题 I have written below code to display employee-manager reporting, i have group by so that all employees under one manager should be shown, but it is showing only 1 employee. what i am missing ? LINQ Group By Logic- var emp = (from m in employee group m by m.ManagerId into g join e1 in employee on g.FirstOrDefault().ManagerId equals e1.EmpId into temp from t1 in temp.DefaultIfEmpty() select new { EmpId = g.FirstOrDefault().EmpId, EmployeeName = g.FirstOrDefault().EmployeeName, Gender = g

Distinct Column in SelectList for dropdown list

和自甴很熟 提交于 2021-01-29 11:29:35
问题 I am retrieving distinct values from a table against only one column. when I debug ViewData in controller it has retrieved the values perfectly, but in my view it says Object is not referenced. can some one help me out. I think the dataValueField and dataTextField are not named "TeamName" anymore after distinct is applied. how to name selected column of my choice so that below SelectList could work. thanks Controller ViewData["TeamNames"] = new SelectList(_context.ActivityMaps.Select(s => s

Lambda expression with statement body error in previously working code

£可爱£侵袭症+ 提交于 2021-01-29 11:27:24
问题 I need help with this, I have a very important piece of code that I got help with from this thread Here The code is as follows: var pln = db.tabStockPlanners .Where(y => y.ExpectedHarvestDate < addoneyear) .Where(r => r.Published == 1) .Where(f => f.Available == 1); var gruppedList = pln.GroupBy(x => x.ItemID, (key, enumerable) => { return new tabStockPlanner { ItemID = key, ExpectedYieldInTonnes = enumerable.Sum(k => k.ExpectedYieldInTonnes) }; }).OrderByDescending(t => t

Select records which the values of the field are the same

旧城冷巷雨未停 提交于 2021-01-29 09:26:08
问题 I have a list getting from the SQLite database like this: var decisions = _db.decisions.Where(x => x.folder_id == Folder.Id).ToList(); If the values of the 'rec_no' field in this list are the same, I would like to output these records in a foreach loop and remaining records to a separate foreach loop. But I have no idea about it. EDIT 1: I want to output 'rec_no' is 13 in a foreach loop and others are in seperated foreach loop which 'folder_id' is 87. EDIT 2: In the example below, is it

How to convert an IGroupedObservable to IGrouping?

故事扮演 提交于 2021-01-29 08:14:59
问题 I have an observable sequence of elements that have a char Key property, that has values in the range from 'A' to 'E' . I want to group these elements based on this key. After grouping them I want the result to by an observable of groups, so that I can process each group separately. My problem is that I can't find a nice way to preserve the key of each group in the final observable. Here is an example of what I am trying to do: var observable = Observable .Interval(TimeSpan.FromMilliseconds

using linq to find if a text field contains any string in a list

假装没事ソ 提交于 2021-01-29 07:40:04
问题 im running this in asp.net core v3.1 my question is similar to this question: How to use Linq to check if a list of strings contains any string in a list with the specific question relating to the first answer such that filterTags = ["abc", "cd", "efg"] var results = db.People .Where(p => filterTags.Any(tag => p.Tags.Contains(tag))); so basically saying give me results from the db of all People who's Tags field contains any of the filterTags where Tags = a big text field populated by a bunch

MIN and MAX value by string in Entity Framework

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 06:05:53
问题 I have database table. The ORM for it is: public long Id { get; set; } public System.Guid AttrId { get; set; } public System.Guid ProdId { get; set; } public string Value { get; set; } public virtual Attributes Attributes { get; set; } public virtual Products Products { get; set; } As you can see value is a string type. I'm trying to get min and max values by this field (some values represented as double). So here is my method for this: public double[] GetMaxMinVals(Guid attrId) { double[]

SQL Linq Many To Many

别来无恙 提交于 2021-01-29 03:05:26
问题 I have a database with a User's and Role's table. Each user can be in lots of roles, so there is a 'middle' table called UserRoles that simply contains a RoleId and UserId. Now I need to get all the roles attached to a User. public IEnumerable<Role> GetAllRoles(int UserId) { var query = from R in _db.Roles where RolesTbl.UserRoles.UserId == UserId select R; return query; } Now the query above doesnt work, "RolesTbl.UserRoles.UserId" I cant referance it in this way because of the Many to Many.