How to compare List to DB Table using LINQ

2020-04-20 16:01发布

问题:

I have a list<> of phone numbers and I am trying to join that with the corresponding records in the db table and get an order number and a customer ID. Also the list has the whole number as one string and the DB has it broken to area code, prefix, number each as separate fields.

I am fairly new to LINQ, so this is a beyond what I currently know. Any suggestions are GREATLY appreciated.

var tnbrs = new List<string>();

have tried:

    var tntable = tnbrs.Cast<DataSet>();  

    var tntable = tnbrs.AsQueryble();<code>

    var custdata = from c in db.CUSTs  
               join t in tntable on c.NPA + c.NXX + c.LINE_NBR equals t.???  
               select new { c.PON, c.PartnerID };

回答1:

You dont have to cast tnbrs to dataset try this instead

var custdata = from c in db.CUSTs  
               where tnbrs.Contains(c.NPA + c.NXX + c.LINE_NBR)
               select new { c.PON, c.PartnerID };

It generates sql query something like this

SELECT [t0].[PON], [t0].[PartnerID]
FROM [dbo].[CUSTs  ] AS [t0]
WHERE [t0].[NPA]) + [t0].[Nxx] + [t0].[LINE_NBR] IN (@p0, @p1)