Entity Framework seleccionar nombre distinto
¿Cómo puedo hacer esta consulta SQL con Entity Framework?
SELECT DISTINCT NAME FROM TestAddresses
7 answers
Usando la expresión lambda..
var result = EFContext.TestAddresses.Select(m => m.Name).Distinct();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-27 15:36:19
Prueba esto:
var results = (from ta in context.TestAddresses
select ta.Name).Distinct();
Esto te dará un IEnumerable<string>
- puedes llamar a .ToList()
para obtener un List<string>
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-27 15:27:23
La forma que @alliswell mostró es completamente válida, ¡y hay otra manera! :)
var result = EFContext.TestAddresses
.GroupBy(ta => ta.Name)
.Select(ta => ta.Key);
Espero que sea útil para alguien.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-01 15:56:00
product.Select(m => new {m.CategoryId, m.CategoryName}).Distinct();
En este ejemplo no duplicate CategoryID y no CategoryName espero que esto te ayude
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-10 15:33:04
Entity-Framework Select Distinct Name:
Supongamos que si eres uso de vistas en las que se utilizan varias tablas y desea aplicar distinto en ese caso primero debe almacenar el valor en variable y luego puede aplicar Distinto en esa variable como esta....
public List<Item_Img_Sal_VIEW> GetItemDescription(int ItemNo)
{
var Result= db.Item_Img_Sal_VIEW.Where(p => p.ItemID == ItemNo).ToList();
return Result.Distinct().ToList();
}
O puedes probar este Simple Ejemplo
Public Function GetUniqueLocation() As List(Of Integer)
Return db.LoginUsers.Select(Function(p) p.LocID).Distinct().ToList()
End Function
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-04 21:15:56
Use Select().Distinto()
por ejemplo
DBContext db = new DBContext();
var data= db.User_Food_UserIntakeFood .Select( ).Distinct();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-05 11:24:07
Entity-Framework Select Distinct Name:
Supongamos que si desea todos los primeros datos de una columna particular de cada grupo ;
var data = objDb.TableName.GroupBy(dt => dt.ColumnName).Select(dt => new { dt.Key }).ToList();
foreach (var item in data)
{
var data2= objDb.TableName.Where(dt=>dt.ColumnName==item.Key).Select(dt=>new {dt.SelectYourColumn}).Distinct().FirstOrDefault();
//Eg.
{
ListBox1.Items.Add(data2.ColumnName);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-21 08:38:18