Briskstar Technologies
Briskstar Technologies

Reputation: 2253

Using datatable in place of list in linq

I wrote the following query in LINQ:
memberlist is List of membershipuserwrapper

List<usercodes> testlist = new List<usercodes>();
                testlist.Add(new usercodes { usercode = "na02\\srosner" });
                testlist.Add(new usercodes { usercode = "[email protected]" });

var filtered = (from c in memberList
                where (from t in testlist 
                       select t.usercode.ToLower())
                       .Contains(c.UserName.ToLower())
                select c)
                .ToList();

Now, I have DataTable with a list of users in place of testlist and I want to use that DataTable in place of my LINQ query.

Can I use that how to do that in way of DataTable?

Upvotes: 1

Views: 199

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

You should be able to use:

from t in testlist.AsEnumerable()
select t.Field<string>("usercode").ToLower()

in place of the original:

from t in testlist
select t.usercode.ToLower()

Personally, though, I'd say that the List<usercodes> is a vast improvement from a DataTable, and would encourage you to pursue the original version (maybe changing the class-name to UserCodes or similar).

Upvotes: 2

Related Questions