English 中文(简体)
# C # 羊羔 林克 # # C # 羊羔 林克 # # C # ambda, linq #
原标题:C#, lambda, linq

假设我有一个包含 ids 列表的 Int 列表 。 我有一张 linq 表格, 我想返回一个特定的列, 但仅当 linq 表格的 ID 等于 int 列表中的任何 IDs 时 。

到目前为止,我已:

dc.tb_References.SelectMany(n => n.ID == ids).ToList();

在Sql我只写:

SELECT Column_Name from Table where ID in (1,2,3,4)

我到处乱逛,但找不到我要找的东西。有人有建议吗?我想继续用羊羔的表达方式。

最佳回答

您可以使用 < a href=>" "http://msdn.microsoft.com/ en-us/library/ system.linq.enumberable.contains.aspx" rel = "nofollow" >Contains() 方法在身份证列表中。

dc.tb_References.Where(item => ids.Contains(item.ID)).ToList();
问题回答

试试这个

dc.tb_References.Where(n => ids.Contains(n.ID)).ToList();

使用 方法 < code > 使用 包含 方法的 < code> 方法:

dc.tb_References
    .Where(n => theListOfIds.Contains(n.ID))
    .Select(x => x.Column_Name)
    .ToList();

或您能够做到:

var query = from item in dc.tb_References
            where theListOfIds.Contains(item.ID)
            select item.Column_Name;

var list = query.ToList();

选择 many 用于从子列表中选择项目, 然后将所有这些 Ites 都作为列表调出 :

Fruit.Items:   Apple, Pear
Veggies.Items: Carrot, Cabbage

List.Items: Fruit, Veggies

List.Items.SelectMany(x => x.Items)

结果:

Apple, Pear, Carrot, Cabbage

这就是你要找的东西吗?

int[] myIds = {1,4,5,3};
List<int> list = new List<int>();

list.Add(1);
list.Add(5);
list.Add(8);
list.Add(9);
list.Add(10);
list.Add(12);


List<int> select = (from l in list where myIds.Contains(l) select l).ToList();

要生成条款, 您需要将收藏和传递 < code> contains 方法称为 < code > 方法, 并以此方法传递您想要搜索的对象的属性 :

var ids = new int[] { 1, 3 };
var query = from n in dc.tb_References 
            where ids.Contains(n.ID) 
            select n;

以下是生成的 SQL (来自 LinqPad):

DECLARE @p0 Int = 1
DECLARE @p1 Int = 3

SELECT [t0].[ID], [t0].[Foo], [t0].[Bar]
FROM [tb_References] AS [t0]
WHERE [t0].[ID] IN (@p0, @p1)
int ids = new int[]{1,2,3,4};
var list =  (from d in dc.tb_References
             where ids. Contains(d.ID)
             select d. Column_Name).ToList();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签