如何从Linq中的同一表中选择2列? [英] How Select 2 Column From Same Table In Linq?
问题描述
嗨专家
i是linq的新手,所以我默认会写一些查询
i我有一个名为Table1的表,有2列(ID,Name),
现在我想用linq从表中选择2列,
in sql we have has
select * from Table1;
怎么用linq写?
Hi Experts
i am new to linq so i am facing default to write some queries
i am having table called Table1 with 2 column(ID,Name),
Now I want Select the 2 column from the table using linq,
in sql we write has
select * from Table1;
how to write in linq?
推荐答案
我想按照你在SQL查询中说的那样选择所有值
I you want to select all values as you said in SQL query is
select * from Table1
为此您可以这样写:
for this you can write like:
var result = Table1.AsEnumerable().ToList();
如果你想选择特定于某个ID的值(这里我给了1,你可以给出任何值)你可以这样写:
If you want to select values specific to some ID (here I have given 1, you can give any value) you can write like:
var result1 = (from r in Table1.AsEnumerable()
where r.Field<int32>("ID")==1
select r).ToList();
您好,
试试这个
Hi,
Try this
from table in Table1
where table.id == tempIdValue
select new { Id = table.Id, Name = table.Name };
我没有测试过这个。希望这样有效。
问候,
Praneet
I havent tested this. Hope this works.
Regards,
Praneet
从这里开始:
使用LINQ选择多个数据字段!!! [ ^ ]
如何:从多个源(LINQ)填充对象集合 [ ^ ]
其他资源:
101 LINQ示例 [ ^ ]
LINQ - 示例查询 [ ^ ]
Start here:
Select multiple data fields using LINQ!!![^]
How to: Populate Object Collections from Multiple Sources (LINQ)[^]
Other resources:
101 LINQ Samples[^]
LINQ - Sample Queries[^]
这篇关于如何从Linq中的同一表中选择2列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!