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<it em_view>() ;
//this is my custom class that converts list to dataset
CollectionToDataSet<List<i tem_view>> c = new CollectionToDataSet<List<i tem_view>> (query1);
return c.CreateDataSet();
}
but it gives repeating data which is not distinct Can any body help me on this?
thank you
{
//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<it
//this is my custom class that converts list to dataset
CollectionToDataSet<List<i
return c.CreateDataSet();
}
but it gives repeating data which is not distinct Can any body help me on this?
thank you
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();
.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();
ASKER
Hi lucky85 thank you for reply i tried it getting below error
Error 3 'System.Collections.Generi c.List<ite m_view>' does not contain a definition for 'distinct' and no extension method 'distinct' accepting a first argument of type 'System.Collections.Generi c.List<ite m_view>' could be found (are you missing a using directive or an assembly reference?)
Error 3 'System.Collections.Generi
ASKER
hi amenkes it did not solved the problem yet can you explain cast furthur
ASKER
Error 1 Cannot implicitly convert type 'System.Collections.Generi c.IEnumera ble<item_v iew>' to 'System.Collections.Generi c.List<ite m_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<it em_view>() ;
//this is my custom class that converts list to dataset
List<item_view> distinctview = query1.Distinct();
CollectionToDataSet<List<i tem_view>> c = new CollectionToDataSet<List<i tem_view>> (distinctv iew);
return c.CreateDataSet();
}
can anybody help me please
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<it
//this is my custom class that converts list to dataset
List<item_view> distinctview = query1.Distinct();
CollectionToDataSet<List<i
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_cod e == group_code).Distinct();
can you try these two queries and see. If you have added the System.collections.generic s 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();
where item.group_code == group_code
select item).ToList();
IEnumerable<string> ids = list.Select(item.group_cod
can you try these two queries and see. If you have added the System.collections.generic
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.
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;
}
}
}
ASKER
I think yes amenkes is correct i have to group by to select didtinct. i will wait for the code.
thank you
thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you amenkes
where item.group_code == group_code
select item).ToList<item_view>();
list<item_view> distinctview = query1.distinct();
Can you try this query.