SolvedPrivate

Linq to SQL help

Posted on 2013-12-03
5
38 Views
Last Modified: 2016-02-10
Please help.  I need to structure this and would like to leverage this as an example to do this with the best approach I can do with vs 2008.

I have a sql server database.  I have two tables.  To keep it simple lets just say table a and table b.  

We create a folder to hold all are entities.  In it for table a I have the following: lets assume this as table a.  Now the code for table b is the same.  Just for each data in the table.  So can or should I put more code here ..such as restraints...ect.  I ask this as my mind is in MVC and this is my first time doing asp.net since doing MVC4.  ok next is the controller we have or really just a bunch of business logic and linq code.  And other than coding better here is the root of my question.  Skip down to the next code segment to follow this question.  Basically I have two entities.  My controller code needs to reference a join from the other code in my linq query..but I cannot b/c it doesnt exist in the entity.  Here is the code for that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace my.strong.name
{
    [DataContract]
    [Serializable]
    public class Brands
    {

        public Brands() { }

        [DataMember(Name = "BrandID")]
        public Int32 BrandID { get; set; }

        [DataMember(Name = "Name")]
        public String Name { get; set; }

        [DataMember(Name = "Description")]
        public String Description { get; set; }

        [DataMember(Name = "BusinessUnitID")]
        public Int32? BusinessUnitID { get; set; }
    }

}

Open in new window




Notice here that is uses the entity from above.  I am joining another table here.  BusinessUnitName exists in what I would refer to as table b.  Or another entity.  How do I get this in one statement.  I am confused.  Please help.
 public Brands[] GetBrands()
        {
           
               var query = from e in db.tblBrands
                           join bu in db.tblBusinessUnits on e.BusinessUnitID equals bu.BusinessUnitID                         
                           orderby e.BrandID
                           select new Brands
                           {
                               BrandID = e.BrandID,
                               Name = e.Name,
                               Description = e.Description,
                               BusinessUnitID = e.BusinessUnitID,
                               BusinessUnitName = bu.Description
                           };

            return query.ToArray();
            
        }       

Open in new window

0
Comment
Question by:Robb Hill
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
not sure what are you trying to do here...
0
 
LVL 11

Author Comment

by:Robb Hill
Comment Utility
two things ...but the major part of this is righting the linq code correct.


I am doing a join. in the code above between two tables and assigning it to a variable.  

                               BrandID = e.BrandID,
                               Name = e.Name,
                               Description = e.Description,
                               BusinessUnitID = e.BusinessUnitID,

This line of code crashes because BusinessUnitName does not exists in my entity brands.  I need to be able to do this..somehow.  I need these values populated from the database through linq...so they are accessable in my call from an asp.net datasource controll.  Do I modify the entity?  Do I modify this linq query...what do I do?

                               BusinessUnitName = bu.Description
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
Comment Utility
you need to add BusinessUnitName to Brands

public class Brands
    {

        public Brands() { }

        [DataMember(Name = "BrandID")]
        public Int32 BrandID { get; set; }

        [DataMember(Name = "Name")]
        public String Name { get; set; }

        [DataMember(Name = "Description")]
        public String Description { get; set; }

        [DataMember(Name = "BusinessUnitID")]
        public Int32? BusinessUnitID { get; set; }

 [DataMember(Name = "BusinessUnitName")]
        public String BusinessunitName { get; set; }


    }
0
 
LVL 11

Author Comment

by:Robb Hill
Comment Utility
so you are saying  its ok to add the datamember to that entity brands ..even though BusinessUnitName does not come from the table that it represents.
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
Sure, I have done that before. And it is not like BusinessUnitName is completely unrelated or anything. So that's one way of doing things. Another way would be create another entity and make BusinessUnitName a part of that second entity. Two entities, brands and Details could be then joined on BusinessUnitID and then query on that
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now