I'm starting to use Entity Framework Code First.
Suppose to have such POCO's (ultimately simplified):
public class BortStructure
{
public Guid Id { get; set; }
public String Name { get; set; }
}
public class Slot
{
public Guid Id { get; set; }
public String Name { get; set; }
public BortStructure { get; set; }
}
public class SystemType
{
public Guid Id { get; set; }
public String Name {get; set; }
}
public class SlotSystemType
{
public Guid Id { get; set; }
public Slot Slot { get; set; }
public SystemType SystemType {get; set; }
}
and a context
public MyContext : DbContext
{
public DbSet<BortStructure> BortStructures { get; set; }
public DbSet<Slot> Slots{ get; set; }
public DbSet<SystemType> SystemTypes { get; set; }
public DbSet<SlotSystemType> SlotSystemTypes { get; set; }
}
I have a task to get BortStructure by Id with list of attached Slots, each one with list of systemTypes attached.
Using SQL allowed me to do that with some JOIN's:
SELECT BortStructures.Id, BortStructures.Name, Slots.Id,
Slots.Name, SystemType.Id, SystemType.Name FROM
((BortStructures LEFT JOIN Slots ON BortStructures.Id = Slots.BortStructureId)
LEFT JOIN SlotSystemTypes ON SlotSystemTypes.SlotId = Slots.Id)
LEFT JOIN SystemTypes ON SystemTypes.Id = SlotSystemTypes.SystemTypeId
WHERE BortStructures.Id='XXXXXX' ORDER BY Slots.Id, SystemType.Id
But with Entity Framework Code First I don't have any idea howto do that.
If I use
var slotSystemTypes = from sl in MyContext.SlotSystemTypes
where sl.Slot.BortStructure.Id = XXXXXX
orderby sl.Slot.Id, sl.SystemType.Id
select sl;
i, of course, will receive nothing if BortStructure consists of no Slots/Slots without any SystemTypes attached.
Instead of getting BortStructure with empty list of Slots/with Slots, each one with empty list of SystemTypes attached as I expect to get.
Is there any way to archive that with single LINQ query for my database configuration?
You can use join operator example:
string[] categories = new string[]{
"Beverages",
"Condiments",
"Vegetables",
"Dairy Products",
"Seafood" };
List<Product> products = GetProductList();
var q =
from c in categories
join p in products on c equals p.Category
select new { Category = c, p.ProductName };
foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
more samples in: http://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9
Related
Im trying to write in linq something which is easy (for me) in SQL. Any idea how to do something like this:
select Items.IdItem,
Items.Name,
count(1) as Quantity,
SUM(IF(State.IdStatus=1,1,0)) as Availible
from Items
inner join State
on Items.IdItem=State.IdItem
group by Items.IdItem
I wrote something like that:
var result = from items in _context.Items
join state in _context.State on items.IdItem equals State.IdItem
group items by { items.IdItem, items.Name } into g
select new { Name= g.Key.Name, IdItem=g.Key,IdItem, Quantity=g.Count(), Availible= ???? }
Any tips?
Provided you have set your relations in the database and navigational properties in your model (which is done by generators), then in Linq you seldom need joins (for tables that don't have a direct relation).
Second, you are not really after Sum() here, are you. Looking at your sum function and field name, it more looks like you are after "Is Available" check.
var result = from i in _context.Items
group i by i.IdItem into g
select new {
IdItem = g.Key,
Name = g.First().Name,
Quantity = g.Count(),
Available = g.Any(it => it.State.IdStatus == 1)
};
EDIT: if your Sum was intentional, then you can replace the Available part by (it is a bit, right?):
Available = g.Sum(it => it.State.IdStatus)
EDIT: This one is based on your data/model and SQL at top:
var result = from i in _context.Items
select new
{
i.IdItem,
i.Name,
Quantity = i.States.Count(),
Available = i.States.Count(x => x.IdStatus == 1)
};
Sample code and results:
string defaultConString = #"server=.\SQLExpress;Database=SampleDb;Trusted_Connection=yes;";
void Main()
{
var _context = new MyContext(defaultConString);
var result = from i in _context.Items
select new
{
i.IdItem,
i.Name,
Quantity = i.States.Count(),
Available = i.States.Count(x => x.IdStatus == 1)
};
result.Dump(); // linqPad
}
public class MyContext : DbContext
{
public MyContext(string connectionString)
: base(connectionString)
{ }
public DbSet<Item> Items { get; set; }
public DbSet<State> States { get; set; }
}
[Table("Items")]
public partial class Item
{
public Item()
{
this.States = new List<State>();
OnCreated();
}
[Key]
public virtual int IdItem { get; set; }
public virtual string Name { get; set; }
public virtual System.DateTime ImportDate { get; set; }
public virtual System.DateTime ReturnDate { get; set; }
public virtual IList<State> States { get; set; }
partial void OnCreated();
}
[Table("States")]
public partial class State
{
public State()
{
OnCreated();
}
[Key]
public virtual int IdState { get; set; }
public virtual string SmId { get; set; }
public virtual string Number { get; set; }
public virtual int IdItem { get; set; }
public virtual int IdStatus { get; set; }
public virtual Item Item { get; set; }
partial void OnCreated();
}
Result:
IdItem Name Quantity Available
1 Test01 3 2
2 Test02 2 1
3 Test03 1 0
First of all this is my first question in the forum so please excuse me for any writing mistake.
I have 4 tables
attaching the table diagram
What I want is to get list of attraction name joining 'tblattraction' with 'tblattractionmaster' and count of the exact attraction for each place from 'tblattractions' using 'locationid' , I am using entity framework but don't know how to do that,
Disclaimer:
Each location can consist Multiple Places
Each Place can consist Multiple Attractions
What I have tried
return context.tblLocationMasters.Select(t => new details()
{
locationid = t.LocationId,
locationname = t.LocationName,
attractions =t.tblPlaces.SelectMany(a => a.tblAttractions).Select(b => new attractions(){
AttractionName=b.tblAttractionMaster.attractionname//(Not working),
TotalAttractions=0//???
}).ToList()
}).ToList();
I recreated your model (slightly different) using Code First. I came up with the following structure:
public class Location
{
public int LocationId { get; set; }
public string LocationName { get; set; }
public ICollection<Place> Places { get; set; }
}
public class Place
{
public int PlaceId { get; set; }
public string PlaceName { get; set; }
public int LocationId { get; set; }
public Location Location { get; set; }
public ICollection<AttractionPlace> Attractions { get; set; }
}
public class Attraction
{
public int AttractionId { get; set; }
public string AttractionName { get; set; }
}
public class AttractionPlace
{
public int AttractionPlaceId { get; set; }
public int PlaceId { get; set; }
public Place Place { get; set; }
public int AttractionId { get; set; }
public Attraction Attraction { get; set; }
}
Then, I could get the results in the way you needed with the following query:
var query = (from loc in db.Locations
join pla in db.Places.Include(x => x.Attractions) on loc.LocationId equals pla.LocationId
let count = pla.Attractions.Count()
select new
{
loc.LocationId,
loc.LocationName,
Attractions = pla.Attractions.Select(z => new
{
pla.PlaceName,
z.AttractionId,
z.Attraction.AttractionName
}),
AttractionsByPlaceCount = count
});
The query above returns data in this format
Just a side note though: I didn't went further to see the performance of this query. The SQL generated by Linq wasn't that bad, but you should consider analyzing it before actually using it in production.
I have a LINQ query that works in an EF 6 (Code First) project. Now I have migrated the code to EF 7, and this query now throws an exception: ArgumentException: Property 'Int32 ID' is not defined for type 'X.Models.Domain.MadeChoice'
The query:
var madeChoices = from res in X.Instance.Residence
join room in X.Instance.Room on res.ID equals room.Residence.ID
join madeChoice in X.Instance.MadeChoice on room.ID equals madeChoice.Room.ID
where res.ID == residence.ID
select room.MadeChoices;
The MadeChoice class:
public class MadeChoice
{
public virtual int ID { get; set; }
[Required]
public virtual ChoiceGroup Choicegroup { get; set; }
[Required]
public virtual Room Room { get; set; }
[Required]
public virtual Item Item { get; set; }
}
The Room class:
public class Room
{
public virtual int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual Residence Residence { get; set; }
public virtual RoomType RoomType { get; set; }
public virtual List<MadeChoice> MadeChoices { get; set; }
// Constructors:
public Room()
{
this.MadeChoices = new List<MadeChoice>();
}
}
The Residence class:
public class Residence
{
public int ID { get; set; }
public string ApartmentNumber { get; set; }
public static IQueryable<List<MadeChoice>> GetMadeChoices(Residence residence)
{
var madeChoices = from res in X.Instance.Residence
join room in X.Instance.Room on res.ID equals room.Residence.ID
join madeChoice in X.Instance.MadeChoice on room.ID equals madeChoice.Room.ID
where res.ID == residence.ID
select room.MadeChoices;
System.Diagnostics.Debug.Write("MadeChoices.Count: ");
System.Diagnostics.Debug.WriteLine(madeChoices.Count());
foreach (var madechoice in madeChoices)
{
System.Diagnostics.Debug.Write("MadeChoice.Count: ");
System.Diagnostics.Debug.WriteLine(madechoice.Count());
}
return madeChoices;
}
// Navigational properties:
public virtual List<Room> Rooms { get; set; }
public virtual ResidenceType ResidenceType { get; set; }
public virtual List<Tenant> Tenants { get; set; }
public virtual List<PeriodResidenceDeadline> PeriodResidenceDeadline { get; set; }
// Constructors:
public Residence()
{
this.Rooms = new List<Room>();
this.Tenants = new List<Tenant>();
this.PeriodResidenceDeadline = new List<PeriodResidenceDeadline>();
}
}
Originally, the ID was not virtual but it didn't affect the error. The database looks as in EF 6. The relationships are one-to-many.
I use EF 7.0.0-rc1-final.
Any hints?
Thanks in advance,
Peter
As EF team said, EF Core RC1 doesn't handle complex sub-types (see their roadmap here https://github.com/aspnet/EntityFramework/wiki/Roadmap#in-progress for Query)
For querying, this will be handle in the EF Core 1.0 RTM version.
In the meantime, you can use one of the solution I enumerate here : EF Core fluent mapping to inner object properties (the problem is the same for mapping)
I'm trying to get a collection of objects using a Linq query that would include child objects. I can do an include on the main table and get results. But if I do an include on one of the tables I join to, I do not get the object that should be returned by the include.
Here are my models:
public class RequestReviewViewModel
{
public Guid RequestId { get; set; }
public Guid ResourceToReviewId { get; set; }
public Guid ReviewRequiredId { get; set; }
public ReviewRequired ReviewRequired { get; set; }
}
public class Required : BaseEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid RequiredId { get; set; }
[Display(Name = "Review Name")]
public int ReviewNameId { get; set; }
[ForeignKey("ReviewNameId")]
public ReviewName ReviewName { get; set; }
}
Here's the linq query I'm trying. The line Required.Include(revr => revr.ReviewName) doesn't seem to do anything:
var requests = (from req in Request.Include(rr => rr.Resource)
join revreq in Required.Include(revr => revr.ReviewName)
on req.ReviewRequiredId equals revreq.RequiredId
where req.IsActive
select new RequestReviewViewModel
{
RequestId = req.RequestId,
ResourceToReviewId = req.ResourceToReviewId,
ReviewRequiredId = req.ReviewRequiredId,
Required = revreq
};
requests.FirstOrDefault().Required.ReviewName.Dump();
While requests.FirstOrDefault().Required.ReviewNameId has a value, the ReviewName object is null. The relationship is in the database, and was created by Code First.
Your ReviewName is not declared virtual which enables eager/lazy loading (and automatic change-tracking to be complete). Add virtual and it should work:
public virtual ReviewName ReviewName { get; set; }
I am displaying a record from my database. The record pulls data from other tables and uses a Int in the main table to represent the value so Item table has a Division equal to 1 and the Division table 1 = ALL . Now that i am displaying the records i am trying to turn the 1 into all. All the ID fields show the int. Which is what my code is telling it to do. But i am trying to display the name and when i do that i get a lot of red. It cannot find the name. CatagoryID should be CategoryName.
Hope that makes sense.
if (!IsPostBack)
{
string v = Request.QueryString["ContactID"];
int itemid;
int.TryParse(v, out itemid);
var customerInfo = GetCustomerInfo(itemid);
CONTACTID.Text = customerInfo[0].ContactID.ToString();
ContactTitle.Text = customerInfo[0].ContactTitlesID.ToString();
ContactNameB.Text = customerInfo[0].ContactName;
DropDownAddCategory.Text = customerInfo[0].CategoryID.ToString();
DDLAddDivision.Text = customerInfo[0].DivisionID.ToString();
ContactPhoneBox.Text = customerInfo[0].ContactOPhone;
ContactCellBox.Text = customerInfo[0].ContactCell;
ContactEmailBox.Text = customerInfo[0].ContactEmail;
CextB.Text = customerInfo[0].Ext;
}
private List<Solutions.Models.Contact> GetCustomerInfo(int itemid)
{
using (ItemContext context = new ItemContext())
{
return (from c in context.Contacts
where c.ContactID == itemid
select c).ToList();
}
}
This is the model
public class Contact
{
[ScaffoldColumn(false)]
public int ContactID { get; set; }
public System.DateTime ContactCreated { get; set; }
public string ContactName { get; set; }
public int? ContactTitlesID { get; set; }
public string ContactOPhone { get; set; }
public bool cApproved { get; set; }
public string User { get; set; }
public string ContactCell { get; set; }
public string ContactEmail { get; set; }
public int? DivisionID { get; set; }
public int? CategoryID { get; set; }
[StringLength(5)]
public string CExt { get; set; }
public virtual Division Division { get; set; }
public virtual Category Category { get; set; }
public virtual ContactTitle ContactTitle { get; set; }
public string Ext { get; set; }
}
With Entity Framework you can include related entities in query results:
return (from c in context.Contacts.Include("Catagory")
where c.ContactID == itemid
select c).ToList();
This will return contacts with Catagory objects: customerInfo.Catagory.CategoryName
BTW instead of returning list of contacts and selecting first one by index (thus possibly having index out of range exception), modify your method to return first contact (or default, if not found):
private Solutions.Models.Contact GetCustomerInfo(int itemid)
{
return (from c in context.Contacts.Include("Catagory")
where c.ContactID == itemid
select c).FirstOrDefault();
}
And use it this way:
var customerInfo = GetCustomerInfo(itemid);
if (customerInfo != null)
{
CONTACTID.Text = customerInfo.ContactID.ToString();
// etc
}
Are you using LINQ to SQL or Entity Framework? Check your model again and make sure the relationship between the two tables are setup correctly. The relationship may be missing from the model, and causing this problem.