Answered I need help about using LINQ ?

  • Wednesday, December 12, 2012 9:16 AM
     
      Has Code

    hi . i have this code 

     public IQueryable<AllVehicleView> GetAllVehicles()
            {
                var dataContext = new HostDBEntities();
                var allVehicles = from s in dataContext.AllVehicles
                                  select new AllVehicleView { ID = s.ID, VehicleName = s.VehicleName, BelongTo = s.BelongTo};
                return allVehicles;
            }  

    this code return Table called AllVehicles and its contain  column called BelongTo . how can i use Distinct() . to return no duplicate values from BelongTo 

    thank you so much my helpers 


    Hesham Hemdan Saleh

All Replies

  • Wednesday, December 12, 2012 9:52 AM
     
     
    Just to clarify it: You want to return only one car, even when it's owner has more than one?
  • Wednesday, December 12, 2012 10:17 AM
     
     

    i need to return all values as i mentioned except the the BelongTo , i want Belong to be not repeated values here is my table 

    AllVehicles Table 

    VehicleID , VehicleName , BelongTo

    1                jackson           office1

    2                michael           office1

    3                riky                 office1

    4               jacky               office2

    5               piter                office2

    i have a dropdown box and its bind only BelongTo column and i want just show me each office with unique name , like dont have to repeat office1 , 3 times because its with 3 rows in table

    i hope you understand me thank you stefan


    Hesham Hemdan Saleh

  • Wednesday, December 12, 2012 10:27 AM
     
     Proposed Answer Has Code

    Hi,

    If you only want to bind the BelongsTo, why include the ID and VehicleName in the returned IQueryable?

    To get distinct BelongsTo values, use this:

    allVehicles.Select(avv => avv.BelongsTo).Distinct();

    Hope this helps


    Please mark the best replies as answers
    Blog: bloggingabout.net/blogs/rick
    Twitter: @rickvdbosch

  • Wednesday, December 12, 2012 10:51 AM
     
      Has Code

    try this code 

    var results = from s in datacontext.AllVehicles
                  group s.ID by s.BelongTo into g
                  select new { Id= g.Key, Cars = g.ToList() };

    good luck 




    Please mark the post as an answer that helps/solves your problem.

  • Wednesday, December 12, 2012 2:34 PM
     
     
    Just return specific column value from the table using linq and bind it to the dropdownbox as said above

    Mark it as helpful if so! Thanks

  • Wednesday, December 12, 2012 3:30 PM
     
     

    You can do it like Rick wrote.

    BUT: Your data context should provide this information by itself using the database to optimize this kind of query. Cause it is a bad practice to load that entire table for extracting a value which should by easier queried in the database backend as view.

  • Thursday, December 13, 2012 5:21 AM
     
     

    Hi Hesham,

    Are you using entity framework or LINQ to SQL ?

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Thursday, December 13, 2012 9:18 PM
     
     
    Hey , first i would like to thank all of you . and i will use most of your help codes and back to you later i just need to modify my code and see the result .

    Hesham Hemdan Saleh

  • Thursday, December 13, 2012 9:22 PM
     
     
    Lise , am using Entity ,

    Hesham Hemdan Saleh

  • Thursday, December 13, 2012 9:41 PM
     
      Has Code

    ok Guys , lets make my example easier .

    here is my code with only BelongTo

    public IQueryable<AllVehicleView> GetAllVehicles()
            {
                var dataContext = new HostDBEntities();
                var allVehicles = from s in dataContext.AllVehicles
                               
                                 select  new AllVehicleView { BelongTo = s.BelongTo };
                return allVehicles;
            }     
    

    all i need is just return the BelongTo with no duplicate values


    Hesham Hemdan Saleh

  • Thursday, December 13, 2012 10:06 PM
     
     Answered Has Code

    public IQueryable<AllVehicleView> GetAllVehicles() { var dataContext = new HostDBEntities(); var allVehicles = (from s in dataContext.AllVehicles

    select new AllVehicleView { BelongTo = s.BelongTo }).distinct(); return allVehicles; }

    Try that.

    JP Cowboy Coders Unite!

    • Marked As Answer by HESHAM HEMDAN Friday, December 14, 2012 4:50 PM
    •  
  • Friday, December 14, 2012 1:23 AM
     
     
    Lise , am using Entity ,

    Hesham Hemdan Saleh

    Okay, so I will move this thread to the most related forum:ADO.NET Entity Framework and LINQ to Entities for better support.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.