Distinct Column in SelectList for dropdown list - entity-framework

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.TeamName).Distinct(), "TeamName", "TeamName", model.TeamName);
View
<select asp-for="TeamName" class="form-control" asp-items="ViewBag.TeamNames"></select>

Assuming TeamName is string type.
_context.ActivityMaps.Select(s => s.TeamName).Distinct().ToList()
So the above query just return a list of string, and it doesn't have TeamName property.
Just remove the dataValueField and dataTextField.
ViewData["TeamNames"] = new SelectList(_context.ActivityMaps.Select(s => s.TeamName).Distinct().ToList(), model.TeamName);
Update:
var names = _db.ActivityMaps.Select(s => s.TeamName).Distinct().Select(n => new { TeamName = n }).ToList();
ViewData["TeamNames"] = new SelectList(names, "TeamName", "TeamName", model.TeamName);

Related

How to add item or value in assigned list to var?

I tried many ways but couldn't get the solution.
// linq query to getting 3 fields from table
var listData = (from row in db.table
orderby row.No ascending
select new {
row.Id,
row.No,
row.Type
}).ToList();
I want to add item or value to listData.
try Code:
var listData = (from row in db.table
select new
{
Id= row.Id,
No= row.No,
Type= row.Type
}
).OrderBy(c=>c.No).ToList();

Entity Framework - Group By Sum

This is a relatively new area for me. I have two entities: Inquiries and Categories.
Each Inquiry has a category and a property indicating an integer value (called TotalTimeSpent), and each category has multiple inquiries.
What I want to do is generate a view that is grouped by categories while summing all the inquiries in that one category.
My code at the moment is:
var catgroup = db.CATEGORies.GroupBy(i => i.CATNAME).Sum(c => c.)
Obviously each grouping has multiple inquiries in it, so when I am doing the c=> c. it is not bringing back the property that I need to sum with only the group extensions (like FirstOrDefault).
How do I continue from here so I can sum TotalTimeSpent for each inquiry based on the above grouping?
Try this instead:
var catgroup = db.CATEGORies.GroupBy(c => c.CATNAME)
.Select(g => new {
g.Key,
SUM = g.Sum(s => s.Inqueries.Select(t => t.TotalTimeSpent).Sum())
});

Entity framework return items from list that not contained in a table

I am new using entity framework
i have a list of form numbers and i want to search from a table for that list of form numbers and then return form numbers from the list that not contains in the table
i tried this query it gave me half of the work:
here is my list:
var strPurchaseFormNoList= new List<string> { "1", "2", "3" }
and this is my query:
var checkPurchaseAccount = vcEntities.VcUserAccountTbls.Where(x =>
!strPurchaseFormNoList.Contains(x.FormNo))
how to select form numbers in the list that not include from the table ?
you're looking for the "Except" extension.
var reject_list= vcEntities.VcUserAccountTbls.Where(p =>strPurchaseFormNoList.Contains(p.FormNo))
var checkPurchaseAccount = vcEntities.VcUserAccountTbls.Except(reject_list);

How to Convert list into other list using entityframework

I have two model classes first one is "Ticket" and other is "ExpTicket" there is only one additional column/properties in ExpTicket and others properties are same in both lists. I extract the data from database in the form of ExpTicket list and then re-assign all column/properties to Ticket List except one additional column which does not exist in Ticket list.
But iam unable to assign data from "ExpTicket" list to "Ticket" list. If anyone can timely help I shall be very thankful. Following is the code which i need to convert From ExpTicket into "Ticket" List but failed. Please help.
var GetTicket = ticketRepository.ExpTicket(r => r.TicketId == TicketId).ToList();
List<Ticket> CTicket = GetTicket.ToList();
First you have:
var GetTicket = ticketRepository.ExpTicket(r => r.TicketId == TicketId).ToList();
Then make a query:
var CTickets = (from t in GetTicket
select new Ticket{field1=t.field1,field2=t.field2...}).ToList();
Or just readjust your model to use TPT (Table per type, Table per type code first) Good luck
var CTickets = new List<Ticket>();
var ExpTicket = ticketRepository.Where(r => r.TicketId == TicketId);
foreach (var ticket in ExpTicket)
{
CTickets.Add(new Ticket { TicketId = ticket.TicketId, ... and so on });
}
// CTickets is the new list of tickets

LINQ To Entities - Items, ItemCategories & Tags

I have an Entity Model that has Items, which can belong to one or more categories, and which can have one or more tags.
I need to write a query that finds all tags for a given ItemCategory, preferably with a single call to the database, as this is going to get called fairly often.
I currently have:
Dim q = (From ic In mContext.ItemCategory _
Where ic.CategoryID = forCategoryID _
Select ic).SelectMany(Function(cat) cat.Items).SelectMany(Function(i) i.Tags) _
.OrderByDescending(Function(t) t.Items.Count).ToList
This is nearly there, apart from it doesn't contain the items for each tag, so I'd have to iterate through, loading the item reference to find out how many items each tag is related to (for font sizing).
Ideally, I want to return a List(Of TagCount), which is just a structure, containing Tag as string, and Count as integer.
I've looked at Group and Join, but I'm not getting anywhere so any help would be greatly appreciated!
This should work (C#, sorry):
var tags = context.Tags.Include("Items").Where(t => t.Items.Any(i => i.ItemCategories.Any(ic => ic.CategoryID == forCategoryID))).ToList();
Second one gets tag name and item count:
var tags = context.Where(t => t.Items.Any(i => i.ItemCategories.Any(ic => ic.CategoryID == forCategoryID))).Select(t => new { TagName = t.Name, ItemCount = t.Items.Count });
We take only tags from items that belong to desired category.
Using VB (I am not sure that it works, I used translator):
Dim tags = context.Where(Function(t) t.Items.Any(Function(i) i.ItemCategories.Any(Function(ic) ic.CategoryID = forCategoryID))).[Select](Function(t) New With { .TagName = t.Name, .ItemCount = t.Items.Count })