What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 12855 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > how to write the count function in join ...
Srujana

how to write the count function in join

Replies: 8 | Posted by: Srujana on 5/26/2012 | Category: ASP.NET Forums | Views: 442 | Status: [Member] | Points: 10  


Hi,
I have two tables product and order,
I need to display number of orders for each product.
For this I want to use count with join but its not working.
which is the correct way to display product name and number of orders using count function.
public Action select()
{
var product = (from u in db.Orders
join d in db.Products on u.ProductID equals d.ProductID
select new

{

d.Name,
count = u.Ord.........


}).ToList();
}


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Sakthi.Singaravel
Sakthi.Singaravel  
Posted on: 5/26/2012 9:14:59 AM
Level: Silver | Status: [Member] | Points: 25

Try this,

public Action select() 


{
var product = (from u in db.Orders
join d in db.Products on u.ProductID equals d.ProductID
select new

{

d.Name,
count = u.Orders.Count()

}).ToList();
}


Regards,
Singaravel M

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Sakthi.Singaravel
Sakthi.Singaravel  
Posted on: 5/26/2012 9:19:48 AM
Level: Silver | Status: [Member] | Points: 25

In this link have query same as u'r query...

See this...

http://forums.asp.net/t/1342535.aspx/1

Regards,
Singaravel M

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Sekar.C
Sekar.C  
Posted on: 5/26/2012 9:31:30 AM
Level: Starter | Status: [Member] | Points: 25

Hi,
send your DB table structure for both tables

Regards
Sekar.c

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Srujana
Srujana  
Posted on: 5/28/2012 1:35:03 AM
Level: Starter | Status: [Member] | Points: 25

public class Product : BaseModel
{
[Key]
public int ProductID { get; set; }

[Required]
//[DisplayName("Group")]
[LocalizedDisplayName("FIRST_NAME", NameResourceType = typeof(Strings))]
public int ProductGroupID { get; set; }

[Required]
[DisplayName("Product")]
public int ProductNameID { get; set; }

[DisplayName("Type")]
public int? ProductTypeID { get; set; }

[Required]
[StringLength(100)]
[DisplayName("Other Name")]
public string Name { get; set; }

[Required]
[StringLength(100)]
[DisplayName("Brand")]
public string BrandName { get; set; }

[Required]
[StringLength(100)]
[DisplayName("Quality")]
public string Quality { get; set; }



[Required]
[StringLength(100)]
[DisplayName("Size - Colli")]
public string ColliSize { get; set; }

[Required]
[StringLength(100)]
[DisplayName("Colli Dimensions")]
public string ColliDimensions { get; set; }

[ScriptIgnore]
public virtual ProductGroup ProductGroup { get; set; }

[ScriptIgnore]
public virtual ProductName ProductName { get; set; }

[ScriptIgnore]
public virtual ProductType ProductType { get; set; }

[ScriptIgnore]
public virtual ColliType ColliType { get; set; }

[ScriptIgnore]
public virtual Country Country { get; set; }

[ScriptIgnore]
public virtual Currency Currency { get; set; }

[ScriptIgnore]
public virtual ICollection<PreferredLocation> PreferredLocations { get; set; }

[ScriptIgnore]
public virtual ICollection<Order> Orders { get; set; }
}
}
public class ProductName : BaseModel
{
[Key]
public int ProductNameID { get; set; }

[DisplayName("Product")]
[StringLength(50)]
public string Name { get; set; }

[Required]
public int ProductGroupID { get; set; }

[ScriptIgnore]
public virtual ProductGroup ProductGroup { get; set; }

[ScriptIgnore]
public virtual ICollection<Product> Products { get; set; }

[ScriptIgnore]
public virtual ICollection<ProductType> ProductTypes { get; set; }
}

public class Order : BaseModel
{
[Key]
public int OrderID { get; set; }

[Required]
[DisplayName("Colli Count")]
public int ColliCount { get; set; }

[Required]
[DisplayName("Order Available?")]
public bool OrderAvailable { get; set; }

[Required]
[DisplayName("Order Shipped")]
public bool OrderShipped { get; set; }

[Required]
[DisplayName("Order Completed")]
public bool OrderCompleted { get; set; }

[DisplayName("Order Cancelled")]
public bool OrderCancelled { get; set; }

[Required]
[ForeignKey("Buyer")]
public int BuyerID { get; set; }

[Required]
[ForeignKey("Seller")]
public int SellerID { get; set; }

[Required]
public int ProductID { get; set; }

//public int OfferID { get; set; }

[Required]
public int PreferredLocationID { get; set; }

public float? Latitude { get; set; }

public float? Longitude { get; set; }

[ScriptIgnore]
public virtual User Buyer { get; set; }

[ScriptIgnore]
public virtual User Seller { get; set; }

[ScriptIgnore]
public virtual Product Product { get; set; }

public virtual PreferredLocation PreferredLocation { get; set; }

}
These are three model classes,
I need to retrieve no.of orders for each product.

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Sabarimahesh
Sabarimahesh  
Posted on: 5/31/2012 9:52:48 AM
Level: Bronze | Status: [Member] | Points: 25

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Sabarimahesh
Sabarimahesh  
Posted on: 5/31/2012 9:53:18 AM
Level: Bronze | Status: [Member] | Points: 25

http://stackoverflow.com/questions/9205702/html-select-items-count-in-asp-net

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Sakthi.Singaravel
Sakthi.Singaravel  
Posted on: 5/31/2012 11:18:35 AM
Level: Silver | Status: [Member] | Points: 25

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Sakthi.Singaravel
Sakthi.Singaravel  
Posted on: 5/31/2012 11:18:45 AM
Level: Silver | Status: [Member] | Points: 25

Srujana, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

Found interesting? Add this to:


 Latest Posts

Write New Post | More ...

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 3:58:00 PM