how to check a list ID exist in context and how to update it in entity framework - entity-framework

i have a question.
we have a list of ID. i want to search all list ID are exist in myContext.Student
and then update the status field of Student entity.
please help me how do it?
thanks.

var matches = myContext.Student.Where(s => list.Contains(s.id));
foreach(var student in matches)
student.status = newStatus;
myContext.SaveChanges();

Related

how to show list of CustomerId in dropdown list

i want to create Customer diary on the base of customerID that present in customer table.
In customerID field i want dropdown list having list of CustomerId's that are present in database
How can i do that ? can someone help me please
var _objAllCustomerIds = null;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
_objAllCustomerIds = context.MyCustomers.Select(customer => customer.Id).ToList();
}
//where AdventureWorksEntities is your DBcontent containing all your entities
//MyCustomers is the entities representation of your customer table
if this is in your .aspx file
then in your .aspx.cs file (assuming you're using code behind) you'd get the list of customer IDs and then bind that list to this dropdown.
Does this answer from another question help?
How to bind the selected value of a DropDownList

C# EF query for typical products - category relationship?

Using entity framework, trying to get some help for a query (prefer method based syntax) for this typical use case:
There is a products table, like:
ownerId
productId
ProductCategoryId
productInfo, etc.
There is a typical product-category-mapping table, like:
somePrimaryKey
ownerId
categoryId
productId
sortOrder
This set up allows one product to be in multiple category, and has its own sort order in each category. Also, we have the "ownerId" in all tables since each owner can only see his own data.
Now, given a categtoryId and ownerId, we need to find all products of this category, sorted by the sortOrder.
Any way how we should write this?
Many Thanks!
You can try to use something along those lines :
// Instanciate your context.
// Do it the way you've already done it, it's here only for example.
DbContext bd = new DbContext();
// The query :
List<Products> listProducts = new List<Products>();
listProducts = db.Products.Where(p => (db.ProductsCategories.Where(pc => pc.CategoryID == categoryID && pc.OwnerID == ownerID).Select(pc => pc.ProductID).OrderBy(pr => pr.sortOrder).ToList()).Contains(p.ProductID)).ToList();
This way use the product-category mapping (categorieID and ownerID are the datas you inject and you keep the sorting.

Find entity related to Lookup field

I have a list of field names from an entity that I know are all lookup fields. I need to know which entity they are lookups to so that I can do further processing of the data.
Is there a way in a plugin that I can find this out just from having the field name?
In a plugin, a lookup field is represented by the EntityReference type.
var lookup = entity.GetAttributeValue<EntityReference>("new_fieldname");
var entityName = lookup.LogicalName;
var entityId = lookup.Id;
var instanceName = lookup.Name;
I think this is what you are looking for:
string lookupEntityName = entity.new_lookupFieldName.LogicalName;
LogicalName will give you the name of the Entity.

Entity Framework - How to Set Association Value?

Let's say I have a Person class and an Order class, with foreign keys in the DB. The EF model will mark Person with a List of Orders and Order with a Person instance.
If I want to set the Person for the Order, do I really have to do it with an instance of Person?
Is there not a slimmed down way to do so, say with just a PersonID ?
To assign Person entity to a Order without loading Person entity, you have to do something like this:
var db = new OneToManyEntities();
var Order = new Order { OrderId = 100, OrderName = "Order name" };
Order. PersonReference.EntityKey = new EntityKey("OneToManyEntities.Person ","PersonID",10);
db.AddToOrders(Order);
db.SaveChanges();
Puzzled's answer is correct for EF v1. It's a pain. If you don't mind the extra query, you can set the property succinctly:
int id = 1;
Order.Person = context.Persons.Where(x => x.PersonID == id).FirstOrDefault();
Entity Framework v4 will have "FK Associations", which is a fancy term for directly-settable foreign keys.

Entity Framework: Find EntityKey Values for One-to-Many

In EF, it is possible to write the following line:
orderLine.OrderReference.EntityKey.EntityKeyValues[0].Value
Which results in the ID of the associated OrderReference.
What would be the solution if I wanted to know the ID's of the orderLines associated with an order?
The point of the line you show is to get the ID without loading orderLine.Order. But you can't get the IDs of a collection without loading. So just look at the ID property, either directly or from the context.
// from context
var lineIds = (from o in Context.Orders
where o.Id = someId
from l in o.Lines
select l.Id).AsEnumerable();
// from loaded order
if (!order.Lines.IsLoaded) order.Lines.Load();
var lineIds = from l in order.Lines
select l.Id;