Link to home
Start Free TrialLog in
Avatar of reddy999
reddy999

asked on

converting sql to linq not working

public static DataSet GetItemLists(int group_code)
        {
                          //converting following sql command to linq            
      //         string sql = "SELECT Distinct parent_sku, group_code, parent_sku, description "
      //                + "FROM item_view "
      //                + "WHERE group_code=@group_code ";

            using (WellsMusDataContext dc = new WellsMusDataContext())
           {

               List<item_view> query1 = (from item in dc.item_views
                                     where item.group_code == group_code
                                     select item).Distinct().ToList<item_view>();



               
              //this is my custom class that converts list to dataset
                CollectionToDataSet<List<item_view>> c = new CollectionToDataSet<List<item_view>>(query1);
                return c.CreateDataSet();
 
}
but it gives repeating data which is not distinct Can any body help me on this?
thank you
Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

List<item_views> query1 = (from item in dc.item_views
                                     where item.group_code == group_code
                                     select item).ToList<item_view>();
list<item_view> distinctview = query1.distinct();

Can you try this query.
It is probably just this:
.ToList();

and you may need to cast it.

 List<item_view>  query1 = (from item in dc.item_views
                                     where item.group_code == group_code
                                     select item).Distinct().ToList();
Avatar of reddy999
reddy999

ASKER

Hi lucky85 thank you for reply i tried it getting below error

Error      3      'System.Collections.Generic.List<item_view>' does not contain a definition for 'distinct' and no extension method 'distinct' accepting a first argument of type 'System.Collections.Generic.List<item_view>' could be found (are you missing a using directive or an assembly reference?)      
hi amenkes it did not solved the problem yet can you explain cast furthur
Error      1      Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<item_view>' to 'System.Collections.Generic.List<item_view>'. An explicit conversion exists (are you missing a cast?)      i got above error now with the following code


 public static DataSet GetItemLists(int group_code)
        {
                          //string group_code
           
      //         string sql = "SELECT Distinct parent_sku, group_code, parent_sku, description "
      //                + "FROM item_view "
      //                + "WHERE group_code=@group_code ";

            using (WellsMusDataContext dc = new WellsMusDataContext())
           {

               List<item_view> query1 = (from item in dc.item_views
                                     where item.group_code == group_code
                                     select item).Distinct().ToList<item_view>();

 //this is my custom class that converts list to dataset


               List<item_view> distinctview = query1.Distinct();


               CollectionToDataSet<List<item_view>> c = new CollectionToDataSet<List<item_view>>(distinctview);
                return c.CreateDataSet();

 }
can anybody help me please
List<item_views> query1 = (from item in dc.item_views
                                     where item.group_code == group_code
                                     select item).ToList();
IEnumerable<string> ids = list.Select(item.group_code == group_code).Distinct();

can you try these two queries and see. If you have added the System.collections.generics then there should not be any reference's issue.

List<item_view> query1=  from item in dc.items..Cast<item_view>()  
                                          where item.group_code == group_code
                                          select item;
list<item_view> distinctview = query1.distinct();
https://www.experts-exchange.com/questions/24079889/LINQ-Distinct.html

This link has a good amount of code you need as it is also dealing with list and distinct funtion in linq.
Right now, you need to do a groupby to get the distinct. Below is partial code. I am on a long, important call, so I will get the rest posted when done.
using System;
using System.Collections.Generic;
using System.Linq;

namespace WebApplicationC3
{
    class test
    {
        public string name;
    }
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<test> list = new List<test>();
            list.Add(new test() { name = "adam" });
            list.Add(new test() { name = "adam" });
            list.Add(new test() { name = "bob" });
            list.Add(new test() { name = "adam" });
            list.Add(new test() { name = "bob" });
            list.Add(new test() { name = "carl" });

            var newlist = (from t in list
                     select t).GroupBy(x => x.name).Distinct().ToList();
            List<test> otherList;
            
        }
     
    }
}

Open in new window

I think yes amenkes is correct i have to group by  to select didtinct. i will wait for the code.

thank you
ASKER CERTIFIED SOLUTION
Avatar of Adam Menkes
Adam Menkes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you amenkes