开发者

Linq - Convert VB.Net to C# help with on query

开发者 https://www.devze.com 2022-12-16 06:59 出处:网络
We\'re slowly converting some code from VB.Net to C#, so I\'m trying to learn the correct syntax in C#. Could someone help with this conversion? Basically I pull from a view all values that have not a

We're slowly converting some code from VB.Net to C#, so I'm trying to learn the correct syntax in C#. Could someone help with this conversion? Basically I pull from a view all values that have not already been selected (saved) in another table and do a search on the items.

Employees a,b,c,d are in the Employee table. I have already selected and saved employee a and b. Now, when I re-run the search it should exclude employees a and b and only search for c and d if the search criteria is met.

This works fine in the vb.net query. I'm trying to figure out how to translate.

Thanks for any help! I think it's a very simple translation, I'm just having a bit of trouble with it.

Dim query = From tmp In context.vw_EmployeeDemographics _
     Where Not (From jitrv In context.JITRuleValidations 
     Join e In context.Employees On jitrv.Employee_RecordID Equals e.RecordID _
     Where jitrv.Parent_RecordID = Parent_RecordID _
  Select e.RecordID).Contains(tmp.Parent_RecordID) And 
  context.fn_ConcatName(tmp.FirstName, tmp.MiddleName,
      tmp.LastName).Contains(_master.pSearchValue1) _
      Order By tmp.LastName.ToUpper(), tmp.FirstName.ToUpper() _
  Select FirstName = tmp.FirstName, MiddleName = tmp.MiddleName, 
  LastName = tmp.LastName, RecordID = tmp.EmployeeID, _
DisplayText = context.fn_ConcatName(tmp.FirstName, tmp.MiddleName, tmp.LastName)开发者_运维技巧


Okay, as a verbatim conversion:

var query = from tmp in context.vw_EmployeeDemographics
            where !(from jitrv in context.JITRuleValidations
                    join e in context.Employees
                    on jitrv.Employee_RecordID equals e.RecordID
                    where jitrv.Parent_RecordID == Parent_RecordID
                    select e.RecordID).Contains(tmp.Parent_RecordID)
                  && context.fn_ConcatName(tmp.FirstName, tmp.MiddleName, tmp.LastName)
                            .Contains(_master.pSearchValue1)
            orderby tmp.LastName.ToUpper(), tmp.FirstName.ToUpper()
            select new { tmp.FirstName, tmp.MiddleName, tmp.LastName,
                         RecordID = tmp.EmployeeID,
                         DisplayText = context.fn_ConcatName(tmp.FirstName, 
                                                     tmp.MiddleName, tmp.LastName) };

The bit I'm not sure about is the Select part in the VB - I'm guessing that it automatically creates a new anonymous type (as does the C# query) but I'm not sure.

I think that should do it - but I strongly recommend that you refactor it into something more readable :) Here are a few changes:

var validations = from jitrv in context.JITRuleValidations
                  join e in context.Employees
                  on jitrv.Employee_RecordID equals e.RecordID
                  where jitrv.Parent_RecordID == Parent_RecordID
                  select e.RecordID;

var query = from tmp in context.vw_EmployeeDemographics
            let DisplayText = fn_ConcatName(tmp.FirstName,
                                            tmp.MiddleName, 
                                            tmp.LastName) 
            where !validations.Contains(tmp.Parent_RecordID)
               && DisplayText.Contains(_master.pSearchValue1)
            orderby tmp.LastName.ToUpper(), tmp.FirstName.ToUpper()
            select new { tmp.FirstName, tmp.MiddleName, tmp.LastName,
                         RecordID = tmp.EmployeeID, DisplayText };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号