public class ConvertedReading
{
public int DatePointID { get; set; }
public string DatapointName { get; set; }
public DateTime ReadingDate { get; set; }
public double Value { get; set; }
}
Given a collection below:
IEnumerable<ConvertedReading> readingData;
using c# , write a LINQ statment using lambda syntax to output a collection of the following:
public class DataPointTotal
{
public string DatapointName{get; set;}
public double Total{get; set;}
}
one item should be returned per datepointID
Total should reprenst the sum of convertedReading.valye relevent to each datePointID
I have tried with the following, nit quite shre if it works, i cant seems to get the sum of value right.
var subtotals = from x in ConvertedReading group x by x.DatapointName into g select new { Type = x.DatapointName , SubTotal = g.Sum(x => x.Total) };
Is the syntax correct?
Thanks guys,
The answer is:
var result = readingData.GroupBy(r => r.DataPointID)
.Select(g => new DataPointTotal
{
DataPointName = g.First().DataPointName,
Total = g.Sum(r => r.Value)
});
But I guess that's cheating considering I wrote the question! ;)
var result = db.ConvertedReading.GroupBy(i=>i.DatapointName).Select(g => new DataPointTotal(){DatapointName = g.DatapointName, Total = g.Sum(i=>i.Total)});
Related
I've got a question relating the above mentioned topic.
------Using Dapper and Caliburn Micro-----
At the moment im building a mvvm wpf application which displays a list of orders. These orders need some ressources to be allocated so the workprocess can begin.
The list of orders provide a few buttons for each row (order) to start, pause, finish and to set the status of the order like "is material allocated".
Whats a good practise to save changes of the above steps made via the list to database?
When creating the order I simply pass the values via a buttonclick to a method in database access project.
For the moment lets just talk about the UIOrderModel and the property IsAllocated
Once the Button (the ugly beige one) is clicked the following method fires:
public void MatAllocated(UIOrderModel order) {
order.IsMatAllocated = "Y";
order.IsAllocated = true;
order.StatusFlag = MKDataWork.Library.Enums.StatusFlag.allocated;
OrdersBc.Refresh();
}
Since it's workig so far as it should be I'm on the question how to store the Information about the changed status of Allocation in my database. By way of example it would be kinda easy just to fire an update query (sp) in the method above.
The collection and the database should always have the same state of data.
The UiOrderModel:
public class UIOrderModel {
private UIOrderModel UIorder = null;
public int Code { get; set; }
public UIuserModel OrderedByEmp { get; set; }
public int OrderedByEmpPersId { get; set; }
public string OrderedByEmpName { get; set; }
public List<UIAllocationModel> AllocationList {get;set;}
public DateTime OrderDate { get; set; }
public UIDepartmentModel ForDepartment { get; set; }
public string DepartmentName { get; set; }
public DateTime ExpectedFinishDate { get; set; }
public string Project { get; set; }
public string Commission { get; set; }
public string IsMatAllocated { get; set; } = "N";
public bool IsAllocated { get; set; } = false;
public string Additions { get; set; }
public StatusFlag StatusFlag { get; set; }
public decimal? Quantity { get; set; }
public UnitOfMeasures Unit { get; set; }
public UIitemModel Item { get; set; }
public string ItemName { get; set; }
public string ItemCode { get; set; }
public DateTime FinishedDateTime { get; set; }
public UIuserModel FinishedByEmp { get; set; }
public UIOrderModel() { }
public UIOrderModel(Dictionary<string,object> entries) {
int i = 0;
this.UIorder = this;
this.Code = int.TryParse(entries["Code"].ToString(), out i) ? i : 0;
this.OrderedByEmp = (UIuserModel)entries["OrderedByEmp"];
this.OrderedByEmpPersId = ((UIuserModel)entries["OrderedByEmp"]).PersId;
this.ForDepartment = (UIDepartmentModel)entries["SelectedDepartment"];
this.DepartmentName = ((UIDepartmentModel)entries["SelectedDepartment"]).Bezeichnung;
this.ExpectedFinishDate = (DateTime)entries["ExpectedFinishDate"];
this.Quantity = (decimal?)entries["Quantity"];
this.Unit = (UnitOfMeasures)entries["SelectedUnitOfMeasures"];
this.Item = (UIitemModel)entries["Item"];
this.ItemName = ((UIitemModel)entries["Item"]).ItemName;
this.ItemCode = ((UIitemModel)entries["Item"]).ItemCode;
this.StatusFlag = (StatusFlag)entries["StatusFlag"];
this.Project = (string)entries["Project"];
this.Commission = (string)entries["Commission"];
this.Additions = (string)entries["Additions"];
}
public UIOrderModel(int code,string orderedByEmpName, int orderedByEmpPersId, string departmentName, DateTime expectedFinishDate,
decimal? quantity, UnitOfMeasures unit, string itemname, string itemcode, string project, string commission,
StatusFlag statusFlag, string additions)
{
this.UIorder = this;
this.Code = code;
this.OrderedByEmpPersId = orderedByEmpPersId;
this.OrderedByEmpName = orderedByEmpName;
this.DepartmentName = departmentName;
this.ExpectedFinishDate = expectedFinishDate;
this.Quantity = quantity;
this.Unit = unit;
this.ItemName = itemname;
this.StatusFlag = statusFlag;
this.Project = project;
this.Commission = commission;
this.Additions = additions;
}
public void SaveOrder() {
OrderModel result = (OrderModel)this;
result.SaveOrder();
}
public static explicit operator OrderModel(UIOrderModel uiOrder) {
return new OrderModel()
{
Code = uiOrder.Code,
OrderDate = uiOrder.OrderDate,
OrderedByEmp = (UserModel)uiOrder.OrderedByEmp,
OrderedByEmpName = $"{uiOrder.OrderedByEmp.FirstName} {uiOrder.OrderedByEmp.LastName}",
ExpectedFinishDate = uiOrder.ExpectedFinishDate,
ForDepartment = (DepartmentModel)uiOrder.ForDepartment,
AllocationList = uiOrder.AllocationList?.Select(am => (AllocationModel)am).ToList(),
IsMatAllocated = uiOrder.IsMatAllocated,
Quantity = uiOrder.Quantity,
Unit = uiOrder.Unit,
Item = (ItemModel)uiOrder.Item,
ItemCode = uiOrder.Item.ItemCode,
ItemName = uiOrder.Item.ItemName,
Project = uiOrder.Project,
Commission = uiOrder.Commission,
StatusFlag = uiOrder.StatusFlag,
Additions = uiOrder.Additions
};
}
public static explicit operator UIOrderModel(OrderModel order) {
return new UIOrderModel()
{
Code = order.Code,
OrderDate = order.OrderDate,
OrderedByEmp = (UIuserModel)order.OrderedByEmp,
OrderedByEmpName = $"{order.OrderedByEmp.FirstName} {order.OrderedByEmp.LastName}",
ExpectedFinishDate = order.ExpectedFinishDate,
ForDepartment = (UIDepartmentModel)order.ForDepartment,
AllocationList = order.AllocationList?.Select(am => (UIAllocationModel)am).ToList(),
IsMatAllocated = order.IsMatAllocated,
Quantity = order.Quantity,
Unit = order.Unit,
Item = (UIitemModel)order.Item,
ItemCode = order.ItemCode,
ItemName = order.ItemName,
Project = order.Project,
Commission = order.Commission,
StatusFlag = order.StatusFlag,
Additions = order.Additions
};
}
}
But whats the correct MVVM way to accomplish this in a proper manner?
Thanks for an advice!
Well I've been on vacation, returned and had some time and distance to the question above. Yesterday I chose a kinda simple way to solve it.
I've implemented the command pattern and passed the execution through my UI project to the data access. It's just one query for all kinds of statussteps (and the relating table) and updates of the whole order. Therefore I pass an enum value (action), orderNo and Id of the employee as parameter for the method / query.
I'm not completely sure if it's the best mvvm way but its working in a comfortable and fast way.
I have the following entities (I'll show the properties I'm working with because I don't want to make it larger than needed):
PROPERTY: Where a property can be child of another one and has a 1-1 relationship with GeoLocation and can have multiple Multimedia and Operation
public partial class Property
{
public Property()
{
InverseParent = new HashSet<Property>();
Multimedia = new HashSet<Multimedia>();
Operation = new HashSet<Operation>();
}
public long Id { get; set; }
public string GeneratedTitle { get; set; }
public string Url { get; set; }
public DateTime? DatePublished { get; set; }
public byte StatusCode { get; set; }
public byte Domain { get; set; }
public long? ParentId { get; set; }
public virtual Property Parent { get; set; }
public virtual GeoLocation GeoLocation { get; set; }
public virtual ICollection<Property> InverseParent { get; set; }
public virtual ICollection<Multimedia> Multimedia { get; set; }
public virtual ICollection<Operation> Operation { get; set; }
}
GEOLOCATION: As mentioned, it has a 1-1 relationship with Property
public partial class GeoLocation
{
public int Id { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public long? PropertyId { get; set; }
public virtual Property Property { get; set; }
}
MULTIMEDIA: it can hold multiple Images, with different sizes, for a single Property. The detail here is that Order specifies the order of the images to be shown in the client application, but it doesn't start always with 1. There're some cases where a Property has Multimedia files that starts with 3 or x.
public partial class Multimedia
{
public long Id { get; set; }
public long? Order { get; set; }
public string Resize360x266 { get; set; }
public long? PropertyId { get; set; }
public virtual Property Property { get; set; }
}
OPERATIONS: defines all the operations a Property can have, using OperationType to name this operation. (rent, sell, etc.)
public partial class Operation
{
public Operation()
{
Price = new HashSet<Price>();
}
public long Id { get; set; }
public long? OperationTypeId { get; set; }
public long? PropertyId { get; set; }
public virtual OperationType OperationType { get; set; }
public virtual Property Property { get; set; }
public virtual ICollection<Price> Price { get; set; }
}
public partial class OperationType
{
public OperationType()
{
Operation = new HashSet<Operation>();
}
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Operation> Operation { get; set; }
}
PRICE: defines the price for each operation and the currency type. (i.e.: A property can have the rent option - Operation - for X amount in USD currency, but another price registered for the same Operation in case of use another currency type )
public partial class Price
{
public long Id { get; set; }
public float? Amount { get; set; }
public string CurrencyCode { get; set; }
public long? OperationId { get; set; }
public virtual Operation Operation { get; set; }
}
Said that, I want to get all the records (actually are about 40K-50K), but only for a few properties. As I mentioned before, the Multimedia table can have a lot of records for each Property, but I only need the first one with the smaller Order value and sorted by DatePublished. After that, I need to convert the result into MapMarker object, which is as follows:
public class MapMarker : EstateBase
{
public long Price { get; set; }
public int Category { get; set; }
public List<Tuple<string, string, string>> Prices { get; set; }
}
In order to achieve this, I made the following:
public async Task<IEnumerable<MapMarker>> GetGeolocatedPropertiesAsync(int quantity)
{
var properties = await GetAllProperties().AsNoTracking()
.Include(g => g.GeoLocation)
.Include(m => m.Multimedia)
.Include(p => p.Operation).ThenInclude(o => o.Price)
.Include(p => p.Operation).ThenInclude(o => o.OperationType)
.Where(p => p.GeoLocation != null
&& !string.IsNullOrEmpty(p.GeoLocation.Address)
&& p.GeoLocation.Longitude != null
&& p.GeoLocation.Latitude != null
&& p.StatusCode == (byte)StatusCode.Online
&& p.Operation.Count > 0)
.OrderByDescending(p => p.ModificationDate)
.Take(quantity)
.Select(p => new {
p.Id,
p.Url,
p.GeneratedTitle,
p.GeoLocation.Address,
p.GeoLocation.Latitude,
p.GeoLocation.Longitude,
p.Domain,
p.Operation,
p.Multimedia.OrderBy(m => m.Order).FirstOrDefault().Resize360x266
})
.ToListAsync();
var mapMarkers = new List<MapMarker>();
try
{
foreach (var property in properties)
{
var mapMarker = new MapMarker();
mapMarker.Id = property.Id.ToString();
mapMarker.Url = property.Url;
mapMarker.Title = property.GeneratedTitle ?? string.Empty;
mapMarker.Address = property.Address ?? string.Empty;
mapMarker.Latitude = property.Latitude.ToString() ?? string.Empty;
mapMarker.Longitude = property.Longitude.ToString() ?? string.Empty;
mapMarker.Domain = ((Domain)Enum.ToObject(typeof(Domain), property.Domain)).ToString();
mapMarker.Image = property.Resize360x266 ?? string.Empty;
mapMarker.Prices = new List<Tuple<string, string, string>>();
foreach (var operation in property.Operation)
{
foreach (var price in operation.Price)
{
var singlePrice = new Tuple<string, string, string>(operation.OperationType.Name, price.CurrencyCode, price.Amount.ToString());
mapMarker.Prices.Add(singlePrice);
}
}
mapMarkers.Add(mapMarker);
}
}
catch (Exception ex)
{
throw;
}
return mapMarkers;
}
but the results take more than 14 minutes and this method could be called multiple times in a minute. I want to optimize it to return the results in the less time possible. I alreay tried removing ToListAsync(), but in the foreach loop it takes a lot of time too, and that makes all the sense.
So, what do you think I can do here?
Thanks in advance.
UPDATE:
Here is GetAllProperties() method, I forgot to include this one.
private IQueryable<Property> GetAllProperties()
{
return _dbContext.Property.AsQueryable();
}
And the SQL query that Entity Framework is making against SQL Server:
SELECT [p].[Id], [p].[Url], [p].[GeneratedTitle], [g].[Address], [g].[Latitude], [g].[Longitude], [p].[Domain], (
SELECT TOP(1) [m].[Resize360x266]
FROM [Multimedia] AS [m]
WHERE [p].[Id] = [m].[PropertyId]
ORDER BY [m].[Order]), [t].[Id], [t].[CreationDate], [t].[ModificationDate], [t].[OperationTypeId], [t].[PropertyId], [t].[Id0], [t].[CreationDate0], [t].[ModificationDate0], [t].[Name], [t].[Id1], [t].[Amount], [t].[CreationDate1], [t].[CurrencyCode], [t].[ModificationDate1], [t].[OperationId]
FROM [Property] AS [p]
LEFT JOIN [GeoLocation] AS [g] ON [p].[Id] = [g].[PropertyId]
LEFT JOIN (
SELECT [o].[Id], [o].[CreationDate], [o].[ModificationDate], [o].[OperationTypeId], [o].[PropertyId], [o0].[Id] AS [Id0], [o0].[CreationDate] AS [CreationDate0], [o0].[ModificationDate] AS [ModificationDate0], [o0].[Name], [p0].[Id] AS [Id1], [p0].[Amount], [p0].[CreationDate] AS [CreationDate1], [p0].[CurrencyCode], [p0].[ModificationDate] AS [ModificationDate1], [p0].[OperationId]
FROM [Operation] AS [o]
LEFT JOIN [OperationType] AS [o0] ON [o].[OperationTypeId] = [o0].[Id]
LEFT JOIN [Price] AS [p0] ON [o].[Id] = [p0].[OperationId]
) AS [t] ON [p].[Id] = [t].[PropertyId]
WHERE (((([g].[Id] IS NOT NULL AND ([g].[Address] IS NOT NULL AND (([g].[Address] <> N'') OR [g].[Address] IS NULL))) AND [g].[Longitude] IS NOT NULL) AND [g].[Latitude] IS NOT NULL) AND ([p].[StatusCode] = CAST(1 AS tinyint))) AND ((
SELECT COUNT(*)
FROM [Operation] AS [o1]
WHERE [p].[Id] = [o1].[PropertyId]) > 0)
ORDER BY [p].[ModificationDate] DESC, [p].[Id], [t].[Id], [t].[Id1]
UPDATE 2: As #Igor mentioned, this is the link of the Execution Plan Result:
https://www.brentozar.com/pastetheplan/?id=BJNz9KdQI
Ok, a few things that should help. #1. .Include() and .Select() should in general be treated mutually exclusive.
You are selecting:
p.Id,
p.Url,
p.GeneratedTitle,
p.GeoLocation.Address,
p.GeoLocation.Latitude,
p.GeoLocation.Longitude,
p.Domain,
p.Operation,
p.Multimedia.OrderBy(m => m.Order).FirstOrDefault().Resize360x266
but then in your foreach loop accessing Price and OperationType entities off it.
Edit Updated the example for the collection of operation. (Whups)
Instead I would recommend:
p.Id,
p.Url,
p.GeneratedTitle,
p.GeoLocation.Address,
p.GeoLocation.Latitude,
p.GeoLocation.Longitude,
p.Domain,
Operations = p.Operation.Select( o => new
{
OperationTypeName = o.OperationType.Name,
o.Price.Amount,
o.Price.CurrencyCode
}).ToList(),
p.Multimedia.OrderBy(m => m.Order).FirstOrDefault().Resize360x266
Then adjust your foreach logic to use the returned properties rather than a returned entity and related entity values.
Loading 40-50k records with something like that image field (MultiMedia) is potentially always going to be problematic. Why do you need to load all 50k in one go?
This looks like something that would put markers on a map. Solutions like this should consider applying a radius filter at the very least to get markers within a reasonable radius of a given center point on a map, or if loading a larger area (zoomed out map) calculating regions and filtering data by region or getting a count falling in that region and loading/rendering the locations in batches of 100 or so rather than potentially waiting for all locations to load. Something to consider.
I want to remove the duplicate entries based on the Name field from the list.
Below is my Code..
public class EPM_Contact_Component_Class {
public List<Hospital_Contacts__c> conList { get; set; }
public List<Hospital_Contacts__c> result { get; set; }
public JCT_Hospital_Model__c jctObj { get; set; }
public Set<Hospital_Contacts__c> myset { get; set; }
public EPM_Contact_Component_Class(ApexPages.StandardController controller) { }
public List<Hospital_Contacts__c> getContacts() {
conList = new List<Hospital_Contacts__c>();
jctObj = new JCT_Hospital_Model__c();
jctObj = [Select EPM_Model__r.Name,Hospital__r.Name from JCT_Hospital_Model__c where Id=:ApexPages.currentPage().getParameters().get('Id')];
system.debug(jctObj.EPM_Model__r.Name);
conList = [select Contacts__r.Name,Contacts__r.Email,Contacts__r.Phone from Hospital_Contacts__c where EPM_Model__r.Name =: jctObj.EPM_Model__r.Name and Hospital__r.Name=:jctObj.Hospital__r.Name ORDER BY Contacts__r.Name DESC ];
myset = new Set<Hospital_Contacts__c>();
result = new List<Hospital_Contacts__c>();
for(integer i=0;i<conList.size();i++) {
Hospital_Contacts__c hospi = conList.get(i);
for(integer j=(i+1);j<conList.size();j++) {
Hospital_Contacts__c hospj = conList.get(j);
if(hospi.Contacts__r.Name == hospj.Contacts__r.Name) {
conList.remove(j);
system.debug('REMOVED::'+j);
}
}
}
return conList;
}
}
It works fine with 2 duplicates..When there are more than 2 duplicates, it returns the Name 2 times.
Anybody can help me please?
Change your inner for loop to :
for(integer j=0;j<conList.size();j++)
Change your if condition to:
if(hospi.Contacts__r.Name == hospj.Contacts__r.Name && i!=j)
I solved the issue. Change the Second For loop as
**for(j=i;j<conList.size();j++)**
I have two entity look like this.
public class Post{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "")]
[MaxLength(100, ErrorMessage = "")]
public string Name { get; set; }
[ForeignKey("ParentPost")]
public int? ParentPostId { get; set; }
public virtual Post ParentPost { get; set; }
public ICollection<Post> SubPosts { get; set; }
}
public class User{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "")]
[MaxLength(100, ErrorMessage = "")]
public string Name { get; set; }
[ForeignKey("Post")]
public int PostId { get; set; }
public virtual Post Post { get; set; }
}
I have a list of Users, I want sort Users with level of Post.
for example :
ID---------Name------ParentID
1----------1-----------NULL
2----------1.1----------1
3----------2-----------NULL
4----------1.1.1--------2
5----------1.2----------3
Sort By Level.
1
2
1.1
1.2
1.1.1
UPDATE :
Here is an alternative to my previous answer that don't need you to add properties in Post. Create extension method for Post entity to count level and number based on Post's Name. I assume you give it a consistent naming.
public static class ExtensionMethods
{
public static int Level(this Post post)
{
var name = post.Name;
var level = 0;
level = name.Count(o => o == '.');
return level;
}
public static int number(this Post post)
{
var name = post.Name;
var number = 0;
var split = name.Split('.');
return split.Length > 1 ? int.Parse(split[split.Length - 1]) : int.Parse(name);
}
}
Then you can sort using linq like:
var posts = new List<Post>();
posts.Add(new Post { Name = "1.2" });
posts.Add(new Post { Name = "1.1.1" });
posts.Add(new Post { Name = "2" });
posts.Add(new Post { Name = "1.1" });
posts.Add(new Post { Name = "1" });
foreach (var post in posts)
{
Console.WriteLine(post.Name);
}
Console.WriteLine("\n\nAfter sorting");
var sorted = posts.OrderBy(p => p.Level()).ThenBy(p => p.number());
foreach (var post in sorted)
{
Console.WriteLine(post.Name);
}
PREVIOUS ANSWER :
I can't think of proper solution with current properties of Post entity. I thought of some logic but those aren't perfect, will break in some conditions.
The proper solution I can think is require adding two more properties in Post. Level property to store Post level, and Number property to store the number after last point in Name (1 for post name 1.1.1, 10 for post name 2.1.10, etc..). With those two additional properties you can sort Post like following:
Posts.OrderBy(p => p.Level)
.ThenBy(p => p.Number);
I have a structure as below.
public class CategoryClass
{
public decimal Category_ID { get; set; }
public string Category_Name { get; set; }
//public System.Nullable<char> _Category_Type;
public ObservableCollection<DAL.SubCategoryClass> SubCat { get; set; }
}
public class SubCategoryClass
{
public decimal Sub_Category_ID { get; set; }
public string Sub_Category_Name { get; set; }
public decimal Category_ID { get; set; }
}
I need to fill data using LINQ.
I have some code here, please correct me to solve it.
public ObservableCollection<DAL.CategoryClass> GetCategoryandSubCategory()
{
var cat = from c in dbc.Categories
select new DAL.CategoryClass
{
Category_ID = c.Category_ID,
Category_Name = c.Category_Name,
SubCat = from d in dbc.Sub_Categories
where d.Category_ID == c.Category_ID
select new DAL.SubCategoryClass
{
Sub_Category_ID = d.Sub_Category_ID,
Sub_Category_Name = d.Sub_Category_Name,
Category_ID = d.Category_ID
}
};
}
Also suggest me some examples of validation tech in WPF.
One option is to just return an IEnumerable.
That may serve your needs.
If you need an ObservableCollection then you need to new it.
For List just .ToList()
As for validation one question at time.
Start with searching MSDB for validation.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
System.Diagnostics.Debug.WriteLine(OCint.Count.ToString());
}
List<SimpleClass> baseList = new List<SimpleClass> { new SimpleClass(1), new SimpleClass(2), new SimpleClass(3) };
public IEnumerable<SimpleClass> iEint
{
get { return baseList.Where(x => x.ID < 3).Select(w=> new SimpleClass(w.ID)); }
}
public List<SimpleClass> Lint
{
get { return iEint.ToList(); ; }
}
public ObservableCollection<SimpleClass> OCint
{
get { return new ObservableCollection<SimpleClass>(iEint); }
}
}
public class SimpleClass
{
public Int32 ID { get; private set; }
public SimpleClass ( Int32 id) { ID = id; }
}
public IEnumerable<DAL.CategoryClass> GetCategoryandSubCategory()
{
ObservableCollection<DAL.SubCategoryClass> s = new ObservableCollection<DAL.SubCategoryClass>();
var cat = from c in dbc.Categories
select new DAL.CategoryClass
{
Category_ID = c.Category_ID,
Category_Name = c.Category_Name,
SubCat =s.Add( from d in dbc.Sub_Categories
where d.Category_ID == c.Category_ID
select new DAL.SubCategoryClass
{
Sub_Category_ID = d.Sub_Category_ID,
Sub_Category_Name = d.Sub_Category_Name,
Category_ID = d.Category_ID
}
};).ToList()
return cat ;
}
hope it work with u
If you want GetCategoryandSubCategory() returns ObservableCollection that is refreshed automatically when dbc.Categories changes or CategoryClass (SubCotegory) properties change, you can use my ObservableComputations library. Using that library you can code:
public ObservableCollection<DAL.CategoryClass> GetCategoryandSubCategory()
{
var cat = dbc.Categories.Selecting(c =>
new DAL.CategoryClass
{
Category_ID = c.Category_ID,
Category_Name = c.Category_Name,
SubCat = dbc.Sub_Categories
.Filtering(d => d.Category_ID == c.Category_ID)
.Selecting(d => new DAL.SubCategoryClass
{
Sub_Category_ID = d.Sub_Category_ID,
Sub_Category_Name = d.Sub_Category_Name,
Category_ID = d.Category_ID
})
});
}
To make code above working dbc.Categories and dbc.Sub_Categories must be of type ObservableCollection and all the classes mentioned in the code must implement INotifyPropertyChanged.