استفاده از خواص راهبری در Entity framework بجای Join نویسی
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۷/۰۸ ۱۲:۲۴
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("BornInCityId")]
public virtual City BornInCity { get; set; }
public int BornInCityId { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> People { get; set; }
}
public class MyContext : DbContext
{
public DbSet<City> Cities { get; set; }
public DbSet<Person> People { get; set; }
}
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(MyContext context)
{
var city1 = new City { Name = "city-1" };
var city2 = new City { Name = "city-2" };
context.Cities.Add(city1);
context.Cities.Add(city2);
var person1 = new Person { Name = "user-1", BornInCity = city1 };
var person2 = new Person { Name = "user-2", BornInCity = city1 };
context.People.Add(person1);
context.People.Add(person2);
base.Seed(context);
}
}
public static class Test
{
public static void RunTests()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
using (var context = new MyContext())
{
var peopleAndCitiesList = from person in context.People
join city in context.Cities
on person.BornInCityId equals city.Id
select new
{
PersonName = person.Name,
CityName = city.Name
};
foreach (var item in peopleAndCitiesList)
{
Console.WriteLine("{0}:{1}", item.PersonName, item.CityName);
}
}
}
}
SELECT
[Extent1].[BornInCityId] AS [BornInCityId],
[Extent1].[Name] AS [Name],
[Extent2].[Name] AS [Name1]
FROM [dbo].[People] AS [Extent1]
INNER JOIN [dbo].[Cities] AS [Extent2] ON [Extent1].[BornInCityId] = [Extent2].[Id]
var peopleAndCitiesList = context.People
.Select(person => new
{
PersonName = person.Name,
CityName = person.BornInCity.Name
});
var citiesList = context.Cities.OrderByDescending(x => x.People.Count());
foreach (var item in citiesList)
{
Console.WriteLine("{0}", item.Name);
}
SELECT
[Project1].[Id] AS [Id],
[Project1].[Name] AS [Name]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[People] AS [Extent2]
WHERE [Extent1].[Id] = [Extent2].[BornInCityId]) AS [C1]
FROM [dbo].[Cities] AS [Extent1]
) AS [Project1]
ORDER BY [Project1].[C1] DESC
var peopleAndCitiesList = context.Cities
.Select(city => new
{
InUseCount = city.People.Count(),
CityName = city.Name
});
foreach (var item in peopleAndCitiesList)
{
Console.WriteLine("{0}:{1}", item.CityName, item.InUseCount);
}
SELECT
[Extent1].[Id] AS [Id],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[People] AS [Extent2]
WHERE [Extent1].[Id] = [Extent2].[BornInCityId]) AS [C1],
[Extent1].[Name] AS [Name]
FROM [dbo].[Cities] AS [Extent1]
var cityId = 1; var list = context.People.Where(x => x.BornInCityId == cityId).ToList();
var cityName = "city-1"; var list2 = context.People.Where(x => x.BornInCity.Name == cityName).ToList();
string personName = "user-1";
var result = context.People.Where(p => p.Name == personName).Select(c => c.BornInCity).ToList();var citiesContainPerson = context.Cities.Where(city => city.People.Any(person => person.Name == "user-1")).ToList();
SELECT [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name] FROM [dbo].[Cities] AS [Extent1] WHERE EXISTS (SELECT 1 AS [C1] FROM [dbo].[People] AS [Extent2] WHERE ([Extent1].[Id] = [Extent2].[BornInCityId]) AND (N'user-1' = [Extent2].[Name]) )
public class Contact
{
public int ContactId { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string FatherName { get; set; }
public string Email { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
} public class Phone
{
public int PhoneId { get; set; }
public string PhoneNumber { get; set; }
public string PhoneNote { get; set; }
public string PhoneAddress { get; set; }
public int PhoneTypeId { get; set; }
public virtual PhoneType PhoneType { get; set; }
[ForeignKey("ContactId")]
public virtual Contact Contact { get; set; }
public int ContactId { get; set; }
} var listContacts = db.Contacts.Include(p => p.Phones).AsQueryable();
if (searchContact.ByName)
listContacts = listContacts.Where(c => c.LName.Contains(searchContact.Name));
if (searchContact.ByNumber)
{
listContacts = listContacts.Where(c=>c.);
}
var phonelistmodel = await
listContacts.OrderBy(p => p.ContactId)
.Skip(page * count)
.Take(count)
.Select(c => new ListPhoneNumberViewmodel()
{
ContactId = c.ContactId,
Email = c.Email,
Name = c.FName + " " + c.LName,
Phones = c.Phones
}).ToListAsync(); listContacts = listContacts.Where(c => c.Phones.Any(x => x.PhoneNumber == "1234....."));
listContacts.SelectMany(c=>c.Phones).Where(c=>c.PhoneNumber=="123....")
The entity or complex type 'PWS.DataLayer.Context.Tag' cannot be constructed in a LINQ to Entities query.
return tags.Cacheable(x => x.Select(item => new Tag
{
Id = item.Id,
ArticlesCount = item.Articles.Count(),
Name = item.Name,
CreatedBy = item.CreatedBy,
CreatedOn = item.CreatedOn,
ModifiedBy = item.ModifiedBy,
ModifiedOn = item.ModifiedOn
})).ToList();