Answered by:
distinct values in linq

Question
-
User639567535 posted
i try linq query
<asp:DropDownList ID="regiondrop" runat="server" AutoPostBack="True" onselectedindexchanged="regiondrop_SelectedIndexChanged"> </asp:DropDownList> <br /><br />
i try to show distinct values in drop-down
on page load
i try this query to get values
T1 tea = new T1(); var list1 = tea.t1.ToList(); var list = (from ta in list1 orderby ta.Region select new { ta.Region,ta.RegionID }).Distinct().ToList();
but this shows all same values where as i want distinct values
any solution?
Tuesday, June 21, 2016 8:00 AM
Answers
-
User36583972 posted
Hi Bakhtawar,
Change your code as the below.
var list = (from ta in tt1 select new { RID = ta.RegionID, Region = ta.Region }).ToList(); var list3 = list.DistinctBy(p => new { p.Region, p.RID });
if (!Page.IsPostBack) { regiondrop.DataSource = list; regiondrop.DataTextField = "Region"; regiondrop.DataValueField = "RID"; regiondrop.DataBind(); }
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 22, 2016 7:53 AM
All replies
-
User-821857111 posted
You need to use the debugger to see what value is being passed to the Convert.ToInt32 method. It may not be what you expect.
Tuesday, June 21, 2016 8:41 AM -
User639567535 posted
You need to use the debugger to see what value is being passed to the Convert.ToInt32 method. It may not be what you expect.
please check update question
Tuesday, June 21, 2016 8:44 AM -
User-821857111 posted
Please don't use the Edit button to completely change your question. If your original question is resolved, mark the thread as answered then start a new thread for the new question.
Tuesday, June 21, 2016 10:16 AM -
User639567535 posted
the question i edit is not resolved but now i post a new quest
Wednesday, June 22, 2016 4:16 AM -
User36583972 posted
Hi Bakhtawar,
You can try the following two methods.
1: Define a TestTTComparare class.
TestTTComparare Class:
class TestTTComparare : IEqualityComparer<TestTT> { public bool Equals(TestTT x, TestTT y) { if (Object.ReferenceEquals(x, y)) return true; if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; return x.Region == y.Region && x.RegionID == y.RegionID; } public int GetHashCode(TestTT number) { if (Object.ReferenceEquals(number, null)) return 0; int hashTextual = number.Region == null ? 0 : number.Region.GetHashCode(); int hashDigital = number.RegionID.GetHashCode(); return hashTextual ^ hashDigital; } }
var list2 = tt1.ToList().Distinct(new TestTTComparare());
2: Define a DistinctBy method.public static class LinqExtendsDis { public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> seenKeys = new HashSet<TKey>(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } } } }
var list3 = tt1.DistinctBy(p => new { p.Region, p.RegionID });
Best Regards,
Yohann Lu
Wednesday, June 22, 2016 6:27 AM -
User639567535 posted
hi install morelinq and add reference file
then i try this
protected void Page_Load(object sender, EventArgs e) { T1 tea = new T1(); var list1 = tea.table1.ToList(); var list = (from ta in list1 select new { RID = ta.RegionID, Region = ta.Region }).ToList(); var list3 = list.DistinctBy(p => new { p.Region, p.RegionID }); if(!Page.IsPostBack) { regiondrop.DataSource = list; regiondrop.DataTextField = "Region"; regiondrop.DataValueField = "RegionID"; regiondrop.DataBind(); } }
but there is error in distinctby
Wednesday, June 22, 2016 7:07 AM -
User36583972 posted
Hi Bakhtawar,
Change your code as the below.
var list = (from ta in tt1 select new { RID = ta.RegionID, Region = ta.Region }).ToList(); var list3 = list.DistinctBy(p => new { p.Region, p.RID });
if (!Page.IsPostBack) { regiondrop.DataSource = list; regiondrop.DataTextField = "Region"; regiondrop.DataValueField = "RID"; regiondrop.DataBind(); }
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 22, 2016 7:53 AM