我可以使用linq将表连接到列表吗?

Sak*_*ket 13 linq entity-framework c#-4.0

我有一张表如下:

PersonalDetails

Columns are:

Name  
BankName
BranchName
AccountNo
Address
Run Code Online (Sandbox Code Playgroud)

我有另一个包含'Name'和'AccountNo'的列表.我必须从表中找到所有记录,其中"Name"和"AccountNo"分别出现在给定列表中.

任何建议都会有所帮助.

我做了以下但没有太多用处:

var duplicationhecklist = dataAccessdup.MST_FarmerProfile
                          .Join(lstFarmerProfiles, 
                                t => new { t.Name,t.AccountNo}, 
                                t1 => new { t1.Name, t1.AccountNo}, 
                                (t, t1) => new { t, t1 })
                           .Select(x => new {
                                               x.t1.Name,
                                               x.t1.BankName,
                                               x.t1.BranchName,
                                               x.t1.AccountNo
                                             }).ToList();
Run Code Online (Sandbox Code Playgroud)

lstFarmerProfiles列表在哪里.

Ger*_*old 20

您可能发现无法使用本地实体对象列表加入Entity Framework LINQ查询,因为它无法转换为SQL.我会预先选择帐号上的数据库数据,然后加入内存.

var accountNumbers = lstFarmerProfiles.Select(x => x.AccountNo).ToArray();

var duplicationChecklist = 
        from profile in dataAccessdup.MST_FarmerProfile
                                     .Where(p => accountNumbers
                                                    .Contains(p.AccountNo))
                                     .AsEnumerable() // Continue in memory
        join param in lstFarmerProfiles on 
            new { profile.Name, profile.AccountNo} equals 
            new { param.Name, param.AccountNo}
        select profile
Run Code Online (Sandbox Code Playgroud)

因此,您永远不会将批量数据拉入内存,但您可以继续进行最小的选择.

如果accountNumbers包含数千个项目,您可以考虑使用更好的可扩展的chunky Contains方法.