简体   繁体   English

Linq查询从两个表中获取不同的记录

[英]Linq Query to Get Distinct Records from Two Tables

I have two Tables - tblExpenses and tblCategories as follows 我有两个表 - tblExpenses和tblCategories如下

tblExpenses tblExpenses

ID (PK),
Place,
DateSpent,
CategoryID (FK)

tblCategory tblCategory

ID (PK),
Name

I tried various LINQ approaches to get all distinct records from the above two tables but not with much success. 我尝试了各种LINQ方法来获取上述两个表中的所有不同记录,但没有取得多大成功。 I tried using UNION and DISTINCT but it didnt work. 我尝试使用UNION和DISTINCT,但它没有用。

The above two tables are defined in my Model section of my project which in turn will create tables in SQLite. 上面两个表在我的项目的Model部分中定义,该部分又将在SQLite中创建表。 I need to retrieve all the distinct records from both the tables to display values in gridview. 我需要从两个表中检索所有不同的记录,以在gridview中显示值。

Kindly provide me some inputs to accomplish this task. 请为我提供一些投入来完成这项任务。 I did some research to find answer to this question but nothing seemed close to what I wanted. 我做了一些研究来找到这个问题的答案,但似乎没有什么接近我想要的。 Excuse me if I duplicated this question. 如果我复制这个问题,请原谅。

Here is the UNION, DISTINCT approaches I tried: 这是我试过的UNION,DISTINCT方法:

DISTINCT # ==> Gives me Repetitive values DISTINCT#==>给我重复值

(from exp in db.Table<tblExpenses >()
                                   from cat in db.Table<tblCategory>()                                       
                                   select new { exp.Id, exp.CategoryId, exp.DateSpent, exp.Expense, exp.Place, cat.Name }).Distinct();

UNION # ==> Got an error while using UNION UNION#==>使用UNION时出错

I think union already does the distict when you join the two tables you can try somethin like 我认为当你加入两张桌子时,联盟已经做了一些你可以尝试的东西

 var query=(from c in db.tblExpenses select c).Concat(from c in 
            db.tblCategory select c).Distinct().ToList();

You will always get DISTINCT records, since you are selecting the tblExpenses.ID too. 您将始终获得DISTINCT记录,因为您也选择了tblExpenses.ID ( Unless there are multiple categories with the same ID. But that of course would be really, really bad design. ) 除非有多个具有相同ID的类别。但这当然是非常非常糟糕的设计。

Remember, when making a JOIN in LINQ, both field names and data types should be the same. 请记住,在LINQ中创建JOIN时,字段名称和数据类型应该相同。 Is the field tblExpenses.CategoryID a nullable field? 字段tblExpenses.CategoryID为可空字段?

If so, try this JOIN : 如果是这样,试试这个JOIN

db.Table<tblExpenses>()
.Join(db.Table<tblCategory>(), 
       exp => new { exp.CategoryId }, 
       cat => new { CategoryId = (int?)cat.ID }, 
       (exp, cat) => new { 
                           exp.Id, 
                           exp.CategoryId, 
                           exp.DateSpent, 
                           exp.Expense, 
                           exp.Place, 
                           cat.Name 
                         })
.Select(j => new { 
                   j.Id, 
                   j.CategoryId, 
                   j.DateSpent, 
                   j.Expense, 
                   j.Place, 
                   j.Name 
                 });

You can try this queries: 您可以尝试以下查询:

A SELECT DISTINCT query like this: 像这样的SELECT DISTINCT查询:

SELECT DISTINCT Name FROM tblCategory INNER JOIN tblExpenses ON tblCategory.categoryID = tblExpenses.categoryID;

limits the results to unique values in the output field. 将结果限制为输出字段中的唯一值。 The query results are not updateable. 查询结果不可更新。

or 要么

A SELECT DISTINCTROW query like this: 像这样的SELECT DISTINCTROW查询:

SELECT DISTINCTROW Name FROM tblCategory INNER JOIN tblExpenses ON tblCategory.categoryID = tblExpenses.categoryID;<br/><br/>

looks at the entire underlying tables, not just the output fields, to find unique rows. 查看整个基础表,而不仅仅是输出字段,以查找唯一的行。

reference: http://www.fmsinc.com/microsoftaccess/query/distinct_vs_distinctrow/unique_values_records.asp 参考: http//www.fmsinc.com/microsoftaccess/query/distinct_vs_distinctrow/unique_values_records.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM