Linq differs from two columns and returns as one field

I have a table below

Id username userid departmentname
1   abc     1234    test1
2   abc     3456    test1
3   abc     1234    test2
4   abc     3456    test2
5   def     8989    test1

      

I want to apply different username and userid columns and return two columns as one field in linq

The required output will be lower

abc-1234
abc-3456
def-8989

      

I have tried the following

[System.Web.Script.Services.ScriptMethod()]
            [System.Web.Services.WebMethod]
            public static List<string> getUserDetails(string prefixText, int count)
            {
                List<string> usernames = new List<string>();
                using (DBEntities context = new DBEntities())
                {
                    var distinctUsers = context.counters.Select(m => new { m.UserName, m.UserID }).Distinct().ToList();                
                    var abc = distinctUsers.Select(p => new  { DisplayText = p.UserName + "-" + p.UserID }).ToList();

                    usernames = abc.Where(r => r.DisplayText.StartsWith(prefixText)).ToList();               

                }
 return usernames;
            }

      

But I ended up with a problem. 1) Once I have the required results, I need to filter the list starting with the input line (prefixtext). I got the results I wanted in the abc variable and was able to get a list starting with the input string, but the error I am getting is "Can't implicitly convert a list of types anonymous to a list string. How do I convert an anonymous type list to a list string? 2) Is the above is the approach better? Or is there a better way than this?

Thanks Ashika, I got it like below without using a group in another line of code.

var distinctUsers = context.counters.Select(m => (m.UserName + "-" + m.UserID)).Distinct().ToList();
var abc = distinctUsers.AsParallel().Where(x => x.StartsWith(prefixText)).ToList();

      

By default, the above order is also necessary for me. Does this make the order by default or should I follow something like below to order:

 var distinctUsers = context.counters.Select(m => (m.UserName + "-" + m.UserID)).Distinct().OrderBy(n=>n).ToList();

      

+3


source to share


2 answers


You can try this:

using (DBEntities context = new DBEntities())
                {
                    var distinctUsers = context.counters.Select(m => (m.UserName + "-" + m.UserID )).ToList();              
                      usernames = distinctUsers.GroupBy(g => g).Select(x => x.FirstOrDefault()).ToList();               

                }

      



OR

using (DBEntities context = new DBEntities())
                {                                 
                      usernames = context.counters.Select(m => (m.UserName + "-" + m.UserID )).GroupBy(g => g).Select(x => x.FirstOrDefault()).ToList();               

                }

      

0


source


Try the following:



create table abc1 as
select distinct concat(username,'-',userid) as result from abc
group by username;

      

0


source







All Articles